utf8.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. -- $Id: testes/utf8.lua $
  2. -- See Copyright Notice in file all.lua
  3. -- UTF-8 file
  4. print "testing UTF-8 library"
  5. local utf8 = require'utf8'
  6. local function checkerror (msg, f, ...)
  7. local s, err = pcall(f, ...)
  8. assert(not s and string.find(err, msg))
  9. end
  10. local function len (s)
  11. return #string.gsub(s, "[\x80-\xBF]", "")
  12. end
  13. local justone = "^" .. utf8.charpattern .. "$"
  14. -- 't' is the list of codepoints of 's'
  15. local function checksyntax (s, t)
  16. -- creates a string "return '\u{t[1]}...\u{t[n]}'"
  17. local ts = {"return '"}
  18. for i = 1, #t do ts[i + 1] = string.format("\\u{%x}", t[i]) end
  19. ts[#t + 2] = "'"
  20. ts = table.concat(ts)
  21. -- its execution should result in 's'
  22. assert(assert(load(ts))() == s)
  23. end
  24. assert(not utf8.offset("alo", 5))
  25. assert(not utf8.offset("alo", -4))
  26. -- 'check' makes several tests over the validity of string 's'.
  27. -- 't' is the list of codepoints of 's'.
  28. local function check (s, t, nonstrict)
  29. local l = utf8.len(s, 1, -1, nonstrict)
  30. assert(#t == l and len(s) == l)
  31. assert(utf8.char(table.unpack(t)) == s) -- 't' and 's' are equivalent
  32. assert(utf8.offset(s, 0) == 1)
  33. checksyntax(s, t)
  34. -- creates new table with all codepoints of 's'
  35. local t1 = {utf8.codepoint(s, 1, -1, nonstrict)}
  36. assert(#t == #t1)
  37. for i = 1, #t do assert(t[i] == t1[i]) end -- 't' is equal to 't1'
  38. for i = 1, l do -- for all codepoints
  39. local pi, pie = utf8.offset(s, i) -- position of i-th char
  40. local pi1 = utf8.offset(s, 2, pi) -- position of next char
  41. assert(pi1 == pie + 1)
  42. assert(string.find(string.sub(s, pi, pi1 - 1), justone))
  43. assert(utf8.offset(s, -1, pi1) == pi)
  44. assert(utf8.offset(s, i - l - 1) == pi)
  45. assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi, pi, nonstrict)))
  46. for j = pi, pi1 - 1 do
  47. local off1, off2 = utf8.offset(s, 0, j)
  48. assert(off1 == pi and off2 == pi1 - 1)
  49. end
  50. for j = pi + 1, pi1 - 1 do
  51. assert(not utf8.len(s, j))
  52. end
  53. assert(utf8.len(s, pi, pi, nonstrict) == 1)
  54. assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1)
  55. assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1)
  56. assert(utf8.len(s, pi1, -1, nonstrict) == l - i)
  57. assert(utf8.len(s, 1, pi, nonstrict) == i)
  58. end
  59. local expected = 1 -- expected position of "current" character
  60. for i = 1, l + 1 do
  61. local p, e = utf8.offset(s, i)
  62. assert(p == expected)
  63. expected = e + 1
  64. end
  65. assert(expected - 1 == #s + 1)
  66. local i = 0
  67. for p, c in utf8.codes(s, nonstrict) do
  68. i = i + 1
  69. assert(c == t[i] and p == utf8.offset(s, i))
  70. assert(utf8.codepoint(s, p, p, nonstrict) == c)
  71. end
  72. assert(i == #t)
  73. i = 0
  74. for c in string.gmatch(s, utf8.charpattern) do
  75. i = i + 1
  76. assert(c == utf8.char(t[i]))
  77. end
  78. assert(i == #t)
  79. for i = 1, l do
  80. assert(utf8.offset(s, i) == utf8.offset(s, i - l - 1, #s + 1))
  81. end
  82. end
  83. do -- error indication in utf8.len
  84. local function checklen (s, p)
  85. local a, b = utf8.len(s)
  86. assert(not a and b == p)
  87. end
  88. checklen("abc\xE3def", 4)
  89. checklen("\xF4\x9F\xBF", 1)
  90. checklen("\xF4\x9F\xBF\xBF", 1)
  91. -- spurious continuation bytes
  92. checklen("汉字\x80", #("汉字") + 1)
  93. checklen("\x80hello", 1)
  94. checklen("hel\x80lo", 4)
  95. checklen("汉字\xBF", #("汉字") + 1)
  96. checklen("\xBFhello", 1)
  97. checklen("hel\xBFlo", 4)
  98. end
  99. -- errors in utf8.codes
  100. do
  101. local function errorcodes (s)
  102. checkerror("invalid UTF%-8 code",
  103. function ()
  104. for c in utf8.codes(s) do assert(c) end
  105. end)
  106. end
  107. errorcodes("ab\xff")
  108. errorcodes("\u{110000}")
  109. errorcodes("in\x80valid")
  110. errorcodes("\xbfinvalid")
  111. errorcodes("αλφ\xBFα")
  112. -- calling interation function with invalid arguments
  113. local f = utf8.codes("")
  114. assert(f("", 2) == nil)
  115. assert(f("", -1) == nil)
  116. assert(f("", math.mininteger) == nil)
  117. end
  118. -- error in initial position for offset
  119. checkerror("position out of bounds", utf8.offset, "abc", 1, 5)
  120. checkerror("position out of bounds", utf8.offset, "abc", 1, -4)
  121. checkerror("position out of bounds", utf8.offset, "", 1, 2)
  122. checkerror("position out of bounds", utf8.offset, "", 1, -1)
  123. checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
  124. checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
  125. checkerror("continuation byte", utf8.offset, "\x80", 1)
  126. -- error in indices for len
  127. checkerror("out of bounds", utf8.len, "abc", 0, 2)
  128. checkerror("out of bounds", utf8.len, "abc", 1, 4)
  129. local s = "hello World"
  130. local t = {string.byte(s, 1, -1)}
  131. for i = 1, utf8.len(s) do assert(t[i] == string.byte(s, i)) end
  132. check(s, t)
  133. check("汉字/漢字", {27721, 23383, 47, 28450, 23383,})
  134. do
  135. local s = "áéí\128"
  136. local t = {utf8.codepoint(s,1,#s - 1)}
  137. assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237)
  138. checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s)
  139. checkerror("out of bounds", utf8.codepoint, s, #s + 1)
  140. t = {utf8.codepoint(s, 4, 3)}
  141. assert(#t == 0)
  142. checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1)
  143. checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1)
  144. -- surrogates
  145. assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1)
  146. assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1)
  147. assert(utf8.codepoint("\u{D800}", 1, 1, true) == 0xD800)
  148. assert(utf8.codepoint("\u{DFFF}", 1, 1, true) == 0xDFFF)
  149. assert(utf8.codepoint("\u{7FFFFFFF}", 1, 1, true) == 0x7FFFFFFF)
  150. end
  151. assert(utf8.char() == "")
  152. assert(utf8.char(0, 97, 98, 99, 1) == "\0abc\1")
  153. assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF)
  154. assert(utf8.codepoint(utf8.char(0x7FFFFFFF), 1, 1, true) == (1<<31) - 1)
  155. checkerror("value out of range", utf8.char, 0x7FFFFFFF + 1)
  156. checkerror("value out of range", utf8.char, -1)
  157. local function invalid (s)
  158. checkerror("invalid UTF%-8 code", utf8.codepoint, s)
  159. assert(not utf8.len(s))
  160. end
  161. -- UTF-8 representation for 0x11ffff (value out of valid range)
  162. invalid("\xF4\x9F\xBF\xBF")
  163. -- surrogates
  164. invalid("\u{D800}")
  165. invalid("\u{DFFF}")
  166. -- overlong sequences
  167. invalid("\xC0\x80") -- zero
  168. invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte)
  169. invalid("\xE0\x9F\xBF") -- 0x7FF (should be coded in 2 bytes)
  170. invalid("\xF0\x8F\xBF\xBF") -- 0xFFFF (should be coded in 3 bytes)
  171. -- invalid bytes
  172. invalid("\x80") -- continuation byte
  173. invalid("\xBF") -- continuation byte
  174. invalid("\xFE") -- invalid byte
  175. invalid("\xFF") -- invalid byte
  176. -- empty string
  177. check("", {})
  178. -- minimum and maximum values for each sequence size
  179. s = "\0 \x7F\z
  180. \xC2\x80 \xDF\xBF\z
  181. \xE0\xA0\x80 \xEF\xBF\xBF\z
  182. \xF0\x90\x80\x80 \xF4\x8F\xBF\xBF"
  183. s = string.gsub(s, " ", "")
  184. check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF})
  185. do
  186. -- original UTF-8 values
  187. local s = "\u{4000000}\u{7FFFFFFF}"
  188. assert(#s == 12)
  189. check(s, {0x4000000, 0x7FFFFFFF}, true)
  190. s = "\u{200000}\u{3FFFFFF}"
  191. assert(#s == 10)
  192. check(s, {0x200000, 0x3FFFFFF}, true)
  193. s = "\u{10000}\u{1fffff}"
  194. assert(#s == 8)
  195. check(s, {0x10000, 0x1FFFFF}, true)
  196. end
  197. local x = "日本語a-4\0éó"
  198. check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})
  199. -- Supplementary Characters
  200. check("𣲷𠜎𠱓𡁻𠵼ab𠺢",
  201. {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,})
  202. check("𨳊𩶘𦧺𨳒𥄫𤓓\xF4\x8F\xBF\xBF",
  203. {0x28CCA, 0x29D98, 0x269FA, 0x28CD2, 0x2512B, 0x244D3, 0x10ffff})
  204. local i = 0
  205. for p, c in string.gmatch(x, "()(" .. utf8.charpattern .. ")") do
  206. i = i + 1
  207. assert(utf8.offset(x, i) == p)
  208. assert(utf8.len(x, p) == utf8.len(x) - i + 1)
  209. assert(utf8.len(c) == 1)
  210. for j = 1, #c - 1 do
  211. assert(utf8.offset(x, 0, p + j - 1) == p)
  212. end
  213. end
  214. print'ok'