utf8.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 = utf8.offset(s, i) -- position of i-th char
  40. local pi1 = utf8.offset(s, 2, pi) -- position of next char
  41. assert(string.find(string.sub(s, pi, pi1 - 1), justone))
  42. assert(utf8.offset(s, -1, pi1) == pi)
  43. assert(utf8.offset(s, i - l - 1) == pi)
  44. assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi, pi, nonstrict)))
  45. for j = pi, pi1 - 1 do
  46. assert(utf8.offset(s, 0, j) == pi)
  47. end
  48. for j = pi + 1, pi1 - 1 do
  49. assert(not utf8.len(s, j))
  50. end
  51. assert(utf8.len(s, pi, pi, nonstrict) == 1)
  52. assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1)
  53. assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1)
  54. assert(utf8.len(s, pi1, -1, nonstrict) == l - i)
  55. assert(utf8.len(s, 1, pi, nonstrict) == i)
  56. end
  57. local i = 0
  58. for p, c in utf8.codes(s, nonstrict) do
  59. i = i + 1
  60. assert(c == t[i] and p == utf8.offset(s, i))
  61. assert(utf8.codepoint(s, p, p, nonstrict) == c)
  62. end
  63. assert(i == #t)
  64. i = 0
  65. for c in string.gmatch(s, utf8.charpattern) do
  66. i = i + 1
  67. assert(c == utf8.char(t[i]))
  68. end
  69. assert(i == #t)
  70. for i = 1, l do
  71. assert(utf8.offset(s, i) == utf8.offset(s, i - l - 1, #s + 1))
  72. end
  73. end
  74. do -- error indication in utf8.len
  75. local function check (s, p)
  76. local a, b = utf8.len(s)
  77. assert(not a and b == p)
  78. end
  79. check("abc\xE3def", 4)
  80. check("\xF4\x9F\xBF", 1)
  81. check("\xF4\x9F\xBF\xBF", 1)
  82. -- spurious continuation bytes
  83. check("汉字\x80", #("汉字") + 1)
  84. check("\x80hello", 1)
  85. check("hel\x80lo", 4)
  86. check("汉字\xBF", #("汉字") + 1)
  87. check("\xBFhello", 1)
  88. check("hel\xBFlo", 4)
  89. end
  90. -- errors in utf8.codes
  91. do
  92. local function errorcodes (s)
  93. checkerror("invalid UTF%-8 code",
  94. function ()
  95. for c in utf8.codes(s) do assert(c) end
  96. end)
  97. end
  98. errorcodes("ab\xff")
  99. errorcodes("\u{110000}")
  100. errorcodes("in\x80valid")
  101. errorcodes("\xbfinvalid")
  102. errorcodes("αλφ\xBFα")
  103. -- calling interation function with invalid arguments
  104. local f = utf8.codes("")
  105. assert(f("", 2) == nil)
  106. assert(f("", -1) == nil)
  107. assert(f("", math.mininteger) == nil)
  108. end
  109. -- error in initial position for offset
  110. checkerror("position out of bounds", utf8.offset, "abc", 1, 5)
  111. checkerror("position out of bounds", utf8.offset, "abc", 1, -4)
  112. checkerror("position out of bounds", utf8.offset, "", 1, 2)
  113. checkerror("position out of bounds", utf8.offset, "", 1, -1)
  114. checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
  115. checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
  116. checkerror("continuation byte", utf8.offset, "\x80", 1)
  117. -- error in indices for len
  118. checkerror("out of bounds", utf8.len, "abc", 0, 2)
  119. checkerror("out of bounds", utf8.len, "abc", 1, 4)
  120. local s = "hello World"
  121. local t = {string.byte(s, 1, -1)}
  122. for i = 1, utf8.len(s) do assert(t[i] == string.byte(s, i)) end
  123. check(s, t)
  124. check("汉字/漢字", {27721, 23383, 47, 28450, 23383,})
  125. do
  126. local s = "áéí\128"
  127. local t = {utf8.codepoint(s,1,#s - 1)}
  128. assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237)
  129. checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s)
  130. checkerror("out of bounds", utf8.codepoint, s, #s + 1)
  131. t = {utf8.codepoint(s, 4, 3)}
  132. assert(#t == 0)
  133. checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1)
  134. checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1)
  135. -- surrogates
  136. assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1)
  137. assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1)
  138. assert(utf8.codepoint("\u{D800}", 1, 1, true) == 0xD800)
  139. assert(utf8.codepoint("\u{DFFF}", 1, 1, true) == 0xDFFF)
  140. assert(utf8.codepoint("\u{7FFFFFFF}", 1, 1, true) == 0x7FFFFFFF)
  141. end
  142. assert(utf8.char() == "")
  143. assert(utf8.char(0, 97, 98, 99, 1) == "\0abc\1")
  144. assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF)
  145. assert(utf8.codepoint(utf8.char(0x7FFFFFFF), 1, 1, true) == (1<<31) - 1)
  146. checkerror("value out of range", utf8.char, 0x7FFFFFFF + 1)
  147. checkerror("value out of range", utf8.char, -1)
  148. local function invalid (s)
  149. checkerror("invalid UTF%-8 code", utf8.codepoint, s)
  150. assert(not utf8.len(s))
  151. end
  152. -- UTF-8 representation for 0x11ffff (value out of valid range)
  153. invalid("\xF4\x9F\xBF\xBF")
  154. -- surrogates
  155. invalid("\u{D800}")
  156. invalid("\u{DFFF}")
  157. -- overlong sequences
  158. invalid("\xC0\x80") -- zero
  159. invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte)
  160. invalid("\xE0\x9F\xBF") -- 0x7FF (should be coded in 2 bytes)
  161. invalid("\xF0\x8F\xBF\xBF") -- 0xFFFF (should be coded in 3 bytes)
  162. -- invalid bytes
  163. invalid("\x80") -- continuation byte
  164. invalid("\xBF") -- continuation byte
  165. invalid("\xFE") -- invalid byte
  166. invalid("\xFF") -- invalid byte
  167. -- empty string
  168. check("", {})
  169. -- minimum and maximum values for each sequence size
  170. s = "\0 \x7F\z
  171. \xC2\x80 \xDF\xBF\z
  172. \xE0\xA0\x80 \xEF\xBF\xBF\z
  173. \xF0\x90\x80\x80 \xF4\x8F\xBF\xBF"
  174. s = string.gsub(s, " ", "")
  175. check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF})
  176. do
  177. -- original UTF-8 values
  178. local s = "\u{4000000}\u{7FFFFFFF}"
  179. assert(#s == 12)
  180. check(s, {0x4000000, 0x7FFFFFFF}, true)
  181. s = "\u{200000}\u{3FFFFFF}"
  182. assert(#s == 10)
  183. check(s, {0x200000, 0x3FFFFFF}, true)
  184. s = "\u{10000}\u{1fffff}"
  185. assert(#s == 8)
  186. check(s, {0x10000, 0x1FFFFF}, true)
  187. end
  188. local x = "日本語a-4\0éó"
  189. check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})
  190. -- Supplementary Characters
  191. check("𣲷𠜎𠱓𡁻𠵼ab𠺢",
  192. {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,})
  193. check("𨳊𩶘𦧺𨳒𥄫𤓓\xF4\x8F\xBF\xBF",
  194. {0x28CCA, 0x29D98, 0x269FA, 0x28CD2, 0x2512B, 0x244D3, 0x10ffff})
  195. local i = 0
  196. for p, c in string.gmatch(x, "()(" .. utf8.charpattern .. ")") do
  197. i = i + 1
  198. assert(utf8.offset(x, i) == p)
  199. assert(utf8.len(x, p) == utf8.len(x) - i + 1)
  200. assert(utf8.len(c) == 1)
  201. for j = 1, #c - 1 do
  202. assert(utf8.offset(x, 0, p + j - 1) == p)
  203. end
  204. end
  205. print'ok'