utf8.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. -- calling interation function with invalid arguments
  94. local f = utf8.codes("")
  95. assert(f("", 2) == nil)
  96. assert(f("", -1) == nil)
  97. assert(f("", math.mininteger) == nil)
  98. end
  99. -- error in initial position for offset
  100. checkerror("position out of bounds", utf8.offset, "abc", 1, 5)
  101. checkerror("position out of bounds", utf8.offset, "abc", 1, -4)
  102. checkerror("position out of bounds", utf8.offset, "", 1, 2)
  103. checkerror("position out of bounds", utf8.offset, "", 1, -1)
  104. checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
  105. checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
  106. checkerror("continuation byte", utf8.offset, "\x80", 1)
  107. -- error in indices for len
  108. checkerror("out of bounds", utf8.len, "abc", 0, 2)
  109. checkerror("out of bounds", utf8.len, "abc", 1, 4)
  110. local s = "hello World"
  111. local t = {string.byte(s, 1, -1)}
  112. for i = 1, utf8.len(s) do assert(t[i] == string.byte(s, i)) end
  113. check(s, t)
  114. check("汉字/漢字", {27721, 23383, 47, 28450, 23383,})
  115. do
  116. local s = "áéí\128"
  117. local t = {utf8.codepoint(s,1,#s - 1)}
  118. assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237)
  119. checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s)
  120. checkerror("out of bounds", utf8.codepoint, s, #s + 1)
  121. t = {utf8.codepoint(s, 4, 3)}
  122. assert(#t == 0)
  123. checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1)
  124. checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1)
  125. -- surrogates
  126. assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1)
  127. assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1)
  128. assert(utf8.codepoint("\u{D800}", 1, 1, true) == 0xD800)
  129. assert(utf8.codepoint("\u{DFFF}", 1, 1, true) == 0xDFFF)
  130. assert(utf8.codepoint("\u{7FFFFFFF}", 1, 1, true) == 0x7FFFFFFF)
  131. end
  132. assert(utf8.char() == "")
  133. assert(utf8.char(0, 97, 98, 99, 1) == "\0abc\1")
  134. assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF)
  135. assert(utf8.codepoint(utf8.char(0x7FFFFFFF), 1, 1, true) == (1<<31) - 1)
  136. checkerror("value out of range", utf8.char, 0x7FFFFFFF + 1)
  137. checkerror("value out of range", utf8.char, -1)
  138. local function invalid (s)
  139. checkerror("invalid UTF%-8 code", utf8.codepoint, s)
  140. assert(not utf8.len(s))
  141. end
  142. -- UTF-8 representation for 0x11ffff (value out of valid range)
  143. invalid("\xF4\x9F\xBF\xBF")
  144. -- surrogates
  145. invalid("\u{D800}")
  146. invalid("\u{DFFF}")
  147. -- overlong sequences
  148. invalid("\xC0\x80") -- zero
  149. invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte)
  150. invalid("\xE0\x9F\xBF") -- 0x7FF (should be coded in 2 bytes)
  151. invalid("\xF0\x8F\xBF\xBF") -- 0xFFFF (should be coded in 3 bytes)
  152. -- invalid bytes
  153. invalid("\x80") -- continuation byte
  154. invalid("\xBF") -- continuation byte
  155. invalid("\xFE") -- invalid byte
  156. invalid("\xFF") -- invalid byte
  157. -- empty string
  158. check("", {})
  159. -- minimum and maximum values for each sequence size
  160. s = "\0 \x7F\z
  161. \xC2\x80 \xDF\xBF\z
  162. \xE0\xA0\x80 \xEF\xBF\xBF\z
  163. \xF0\x90\x80\x80 \xF4\x8F\xBF\xBF"
  164. s = string.gsub(s, " ", "")
  165. check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF})
  166. do
  167. -- original UTF-8 values
  168. local s = "\u{4000000}\u{7FFFFFFF}"
  169. assert(#s == 12)
  170. check(s, {0x4000000, 0x7FFFFFFF}, true)
  171. s = "\u{200000}\u{3FFFFFF}"
  172. assert(#s == 10)
  173. check(s, {0x200000, 0x3FFFFFF}, true)
  174. s = "\u{10000}\u{1fffff}"
  175. assert(#s == 8)
  176. check(s, {0x10000, 0x1FFFFF}, true)
  177. end
  178. x = "日本語a-4\0éó"
  179. check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})
  180. -- Supplementary Characters
  181. check("𣲷𠜎𠱓𡁻𠵼ab𠺢",
  182. {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,})
  183. check("𨳊𩶘𦧺𨳒𥄫𤓓\xF4\x8F\xBF\xBF",
  184. {0x28CCA, 0x29D98, 0x269FA, 0x28CD2, 0x2512B, 0x244D3, 0x10ffff})
  185. local i = 0
  186. for p, c in string.gmatch(x, "()(" .. utf8.charpattern .. ")") do
  187. i = i + 1
  188. assert(utf8.offset(x, i) == p)
  189. assert(utf8.len(x, p) == utf8.len(x) - i + 1)
  190. assert(utf8.len(c) == 1)
  191. for j = 1, #c - 1 do
  192. assert(utf8.offset(x, 0, p + j - 1) == p)
  193. end
  194. end
  195. print'ok'