utf8.lua 6.6 KB

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