strings.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. -- $Id: testes/strings.lua $
  2. -- See Copyright Notice in file all.lua
  3. print('testing strings and string library')
  4. local maxi <const> = math.maxinteger
  5. local mini <const> = math.mininteger
  6. local function checkerror (msg, f, ...)
  7. local s, err = pcall(f, ...)
  8. assert(not s and string.find(err, msg))
  9. end
  10. -- testing string comparisons
  11. assert('alo' < 'alo1')
  12. assert('' < 'a')
  13. assert('alo\0alo' < 'alo\0b')
  14. assert('alo\0alo\0\0' > 'alo\0alo\0')
  15. assert('alo' < 'alo\0')
  16. assert('alo\0' > 'alo')
  17. assert('\0' < '\1')
  18. assert('\0\0' < '\0\1')
  19. assert('\1\0a\0a' <= '\1\0a\0a')
  20. assert(not ('\1\0a\0b' <= '\1\0a\0a'))
  21. assert('\0\0\0' < '\0\0\0\0')
  22. assert(not('\0\0\0\0' < '\0\0\0'))
  23. assert('\0\0\0' <= '\0\0\0\0')
  24. assert(not('\0\0\0\0' <= '\0\0\0'))
  25. assert('\0\0\0' <= '\0\0\0')
  26. assert('\0\0\0' >= '\0\0\0')
  27. assert(not ('\0\0b' < '\0\0a\0'))
  28. -- testing string.sub
  29. assert(string.sub("123456789",2,4) == "234")
  30. assert(string.sub("123456789",7) == "789")
  31. assert(string.sub("123456789",7,6) == "")
  32. assert(string.sub("123456789",7,7) == "7")
  33. assert(string.sub("123456789",0,0) == "")
  34. assert(string.sub("123456789",-10,10) == "123456789")
  35. assert(string.sub("123456789",1,9) == "123456789")
  36. assert(string.sub("123456789",-10,-20) == "")
  37. assert(string.sub("123456789",-1) == "9")
  38. assert(string.sub("123456789",-4) == "6789")
  39. assert(string.sub("123456789",-6, -4) == "456")
  40. assert(string.sub("123456789", mini, -4) == "123456")
  41. assert(string.sub("123456789", mini, maxi) == "123456789")
  42. assert(string.sub("123456789", mini, mini) == "")
  43. assert(string.sub("\000123456789",3,5) == "234")
  44. assert(("\000123456789"):sub(8) == "789")
  45. -- testing string.find
  46. assert(string.find("123456789", "345") == 3)
  47. a,b = string.find("123456789", "345")
  48. assert(string.sub("123456789", a, b) == "345")
  49. assert(string.find("1234567890123456789", "345", 3) == 3)
  50. assert(string.find("1234567890123456789", "345", 4) == 13)
  51. assert(not string.find("1234567890123456789", "346", 4))
  52. assert(string.find("1234567890123456789", ".45", -9) == 13)
  53. assert(not string.find("abcdefg", "\0", 5, 1))
  54. assert(string.find("", "") == 1)
  55. assert(string.find("", "", 1) == 1)
  56. assert(not string.find("", "", 2))
  57. assert(not string.find('', 'aaa', 1))
  58. assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
  59. assert(string.len("") == 0)
  60. assert(string.len("\0\0\0") == 3)
  61. assert(string.len("1234567890") == 10)
  62. assert(#"" == 0)
  63. assert(#"\0\0\0" == 3)
  64. assert(#"1234567890" == 10)
  65. -- testing string.byte/string.char
  66. assert(string.byte("a") == 97)
  67. assert(string.byte("\xe4") > 127)
  68. assert(string.byte(string.char(255)) == 255)
  69. assert(string.byte(string.char(0)) == 0)
  70. assert(string.byte("\0") == 0)
  71. assert(string.byte("\0\0alo\0x", -1) == string.byte('x'))
  72. assert(string.byte("ba", 2) == 97)
  73. assert(string.byte("\n\n", 2, -1) == 10)
  74. assert(string.byte("\n\n", 2, 2) == 10)
  75. assert(string.byte("") == nil)
  76. assert(string.byte("hi", -3) == nil)
  77. assert(string.byte("hi", 3) == nil)
  78. assert(string.byte("hi", 9, 10) == nil)
  79. assert(string.byte("hi", 2, 1) == nil)
  80. assert(string.char() == "")
  81. assert(string.char(0, 255, 0) == "\0\255\0")
  82. assert(string.char(0, string.byte("\xe4"), 0) == "\0\xe4\0")
  83. assert(string.char(string.byte("\xe4l\0óu", 1, -1)) == "\xe4l\0óu")
  84. assert(string.char(string.byte("\xe4l\0óu", 1, 0)) == "")
  85. assert(string.char(string.byte("\xe4l\0óu", -10, 100)) == "\xe4l\0óu")
  86. checkerror("out of range", string.char, 256)
  87. checkerror("out of range", string.char, -1)
  88. checkerror("out of range", string.char, math.maxinteger)
  89. checkerror("out of range", string.char, math.mininteger)
  90. assert(string.upper("ab\0c") == "AB\0C")
  91. assert(string.lower("\0ABCc%$") == "\0abcc%$")
  92. assert(string.rep('teste', 0) == '')
  93. assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê')
  94. assert(string.rep('', 10) == '')
  95. if string.packsize("i") == 4 then
  96. -- result length would be 2^31 (int overflow)
  97. checkerror("too large", string.rep, 'aa', (1 << 30))
  98. checkerror("too large", string.rep, 'a', (1 << 30), ',')
  99. end
  100. -- repetitions with separator
  101. assert(string.rep('teste', 0, 'xuxu') == '')
  102. assert(string.rep('teste', 1, 'xuxu') == 'teste')
  103. assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1')
  104. assert(string.rep('', 10, '.') == string.rep('.', 9))
  105. assert(not pcall(string.rep, "aa", maxi // 2 + 10))
  106. assert(not pcall(string.rep, "", maxi // 2 + 10, "aa"))
  107. assert(string.reverse"" == "")
  108. assert(string.reverse"\0\1\2\3" == "\3\2\1\0")
  109. assert(string.reverse"\0001234" == "4321\0")
  110. for i=0,30 do assert(string.len(string.rep('a', i)) == i) end
  111. assert(type(tostring(nil)) == 'string')
  112. assert(type(tostring(12)) == 'string')
  113. assert(string.find(tostring{}, 'table:'))
  114. assert(string.find(tostring(print), 'function:'))
  115. assert(#tostring('\0') == 1)
  116. assert(tostring(true) == "true")
  117. assert(tostring(false) == "false")
  118. assert(tostring(-1203) == "-1203")
  119. assert(tostring(1203.125) == "1203.125")
  120. assert(tostring(-0.5) == "-0.5")
  121. assert(tostring(-32767) == "-32767")
  122. if math.tointeger(2147483647) then -- no overflow? (32 bits)
  123. assert(tostring(-2147483647) == "-2147483647")
  124. end
  125. if math.tointeger(4611686018427387904) then -- no overflow? (64 bits)
  126. assert(tostring(4611686018427387904) == "4611686018427387904")
  127. assert(tostring(-4611686018427387904) == "-4611686018427387904")
  128. end
  129. if tostring(0.0) == "0.0" then -- "standard" coercion float->string
  130. assert('' .. 12 == '12' and 12.0 .. '' == '12.0')
  131. assert(tostring(-1203 + 0.0) == "-1203.0")
  132. else -- compatible coercion
  133. assert(tostring(0.0) == "0")
  134. assert('' .. 12 == '12' and 12.0 .. '' == '12')
  135. assert(tostring(-1203 + 0.0) == "-1203")
  136. end
  137. do -- tests for '%p' format
  138. -- not much to test, as C does not specify what '%p' does.
  139. -- ("The value of the pointer is converted to a sequence of printing
  140. -- characters, in an implementation-defined manner.")
  141. local null = "(null)" -- nulls are formatted by Lua
  142. assert(string.format("%p", 4) == null)
  143. assert(string.format("%p", true) == null)
  144. assert(string.format("%p", nil) == null)
  145. assert(string.format("%p", {}) ~= null)
  146. assert(string.format("%p", print) ~= null)
  147. assert(string.format("%p", coroutine.running()) ~= null)
  148. assert(string.format("%p", io.stdin) ~= null)
  149. assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
  150. assert(string.format("%p", print) == string.format("%p", print))
  151. assert(string.format("%p", print) ~= string.format("%p", assert))
  152. assert(#string.format("%90p", {}) == 90)
  153. assert(#string.format("%-60p", {}) == 60)
  154. assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null)
  155. assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null))
  156. do
  157. local t1 = {}; local t2 = {}
  158. assert(string.format("%p", t1) ~= string.format("%p", t2))
  159. end
  160. do -- short strings are internalized
  161. local s1 = string.rep("a", 10)
  162. local s2 = string.rep("aa", 5)
  163. assert(string.format("%p", s1) == string.format("%p", s2))
  164. end
  165. do -- long strings aren't internalized
  166. local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
  167. assert(string.format("%p", s1) ~= string.format("%p", s2))
  168. end
  169. end
  170. x = '"ílo"\n\\'
  171. assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\')
  172. assert(string.format('%q', "\0") == [["\0"]])
  173. assert(load(string.format('return %q', x))() == x)
  174. x = "\0\1\0023\5\0009"
  175. assert(load(string.format('return %q', x))() == x)
  176. assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) ==
  177. "\0\xe4\0b8c\0")
  178. assert(string.format('') == "")
  179. assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) ==
  180. string.format("%1c%-c%-1c%c", 34, 48, 90, 100))
  181. assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be')
  182. assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023")
  183. assert(tonumber(string.format("%f", 10.3)) == 10.3)
  184. assert(string.format('"%-50s"', 'a') == '"a' .. string.rep(' ', 49) .. '"')
  185. assert(string.format("-%.20s.20s", string.rep("%", 2000)) ==
  186. "-"..string.rep("%", 20)..".20s")
  187. assert(string.format('"-%20s.20s"', string.rep("%", 2000)) ==
  188. string.format("%q", "-"..string.rep("%", 2000)..".20s"))
  189. do
  190. local function checkQ (v)
  191. local s = string.format("%q", v)
  192. local nv = load("return " .. s)()
  193. assert(v == nv and math.type(v) == math.type(nv))
  194. end
  195. checkQ("\0\0\1\255\u{234}")
  196. checkQ(math.maxinteger)
  197. checkQ(math.mininteger)
  198. checkQ(math.pi)
  199. checkQ(0.1)
  200. checkQ(true)
  201. checkQ(nil)
  202. checkQ(false)
  203. checkQ(math.huge)
  204. checkQ(-math.huge)
  205. assert(string.format("%q", 0/0) == "(0/0)") -- NaN
  206. checkerror("no literal", string.format, "%q", {})
  207. end
  208. assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0")
  209. checkerror("contains zeros", string.format, "%10s", "\0")
  210. -- format x tostring
  211. assert(string.format("%s %s", nil, true) == "nil true")
  212. assert(string.format("%s %.4s", false, true) == "false true")
  213. assert(string.format("%.3s %.3s", false, true) == "fal tru")
  214. local m = setmetatable({}, {__tostring = function () return "hello" end,
  215. __name = "hi"})
  216. assert(string.format("%s %.10s", m, m) == "hello hello")
  217. getmetatable(m).__tostring = nil -- will use '__name' from now on
  218. assert(string.format("%.4s", m) == "hi: ")
  219. getmetatable(m).__tostring = function () return {} end
  220. checkerror("'__tostring' must return a string", tostring, m)
  221. assert(string.format("%x", 0.0) == "0")
  222. assert(string.format("%02x", 0.0) == "00")
  223. assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF")
  224. assert(string.format("%+08d", 31501) == "+0031501")
  225. assert(string.format("%+08d", -30927) == "-0030927")
  226. do -- longest number that can be formatted
  227. local i = 1
  228. local j = 10000
  229. while i + 1 < j do -- binary search for maximum finite float
  230. local m = (i + j) // 2
  231. if 10^m < math.huge then i = m else j = m end
  232. end
  233. assert(10^i < math.huge and 10^j == math.huge)
  234. local s = string.format('%.99f', -(10^i))
  235. assert(string.len(s) >= i + 101)
  236. assert(tonumber(s) == -(10^i))
  237. -- limit for floats
  238. assert(10^38 < math.huge)
  239. local s = string.format('%.99f', -(10^38))
  240. assert(string.len(s) >= 38 + 101)
  241. assert(tonumber(s) == -(10^38))
  242. end
  243. -- testing large numbers for format
  244. do -- assume at least 32 bits
  245. local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits
  246. assert(string.sub(string.format("%8x", -1), -8) == "ffffffff")
  247. assert(string.format("%x", max) == "7fffffff")
  248. assert(string.sub(string.format("%x", min), -8) == "80000000")
  249. assert(string.format("%d", max) == "2147483647")
  250. assert(string.format("%d", min) == "-2147483648")
  251. assert(string.format("%u", 0xffffffff) == "4294967295")
  252. assert(string.format("%o", 0xABCD) == "125715")
  253. max, min = 0x7fffffffffffffff, -0x8000000000000000
  254. if max > 2.0^53 then -- only for 64 bits
  255. assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff")
  256. assert(string.format("0x%8X", 0x8f000003) == "0x8F000003")
  257. assert(string.format("%d", 2^53) == "9007199254740992")
  258. assert(string.format("%i", -2^53) == "-9007199254740992")
  259. assert(string.format("%x", max) == "7fffffffffffffff")
  260. assert(string.format("%x", min) == "8000000000000000")
  261. assert(string.format("%d", max) == "9223372036854775807")
  262. assert(string.format("%d", min) == "-9223372036854775808")
  263. assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615")
  264. assert(tostring(1234567890123) == '1234567890123')
  265. end
  266. end
  267. do print("testing 'format %a %A'")
  268. local function matchhexa (n)
  269. local s = string.format("%a", n)
  270. -- result matches ISO C requirements
  271. assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$"))
  272. assert(tonumber(s) == n) -- and has full precision
  273. s = string.format("%A", n)
  274. assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$"))
  275. assert(tonumber(s) == n)
  276. end
  277. for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30,
  278. -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do
  279. matchhexa(n)
  280. end
  281. assert(string.find(string.format("%A", 0.0), "^0X0%.?0*P%+?0$"))
  282. assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0*p%+?0$"))
  283. if not _port then -- test inf, -inf, NaN, and -0.0
  284. assert(string.find(string.format("%a", 1/0), "^inf"))
  285. assert(string.find(string.format("%A", -1/0), "^%-INF"))
  286. assert(string.find(string.format("%a", 0/0), "^%-?nan"))
  287. assert(string.find(string.format("%a", -0.0), "^%-0x0"))
  288. end
  289. if not pcall(string.format, "%.3a", 0) then
  290. (Message or print)("\n >>> modifiers for format '%a' not available <<<\n")
  291. else
  292. assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$"))
  293. assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$"))
  294. end
  295. end
  296. -- testing some flags (all these results are required by ISO C)
  297. assert(string.format("%#12o", 10) == " 012")
  298. assert(string.format("%#10x", 100) == " 0x64")
  299. assert(string.format("%#-17X", 100) == "0X64 ")
  300. assert(string.format("%013i", -100) == "-000000000100")
  301. assert(string.format("%2.5d", -100) == "-00100")
  302. assert(string.format("%.u", 0) == "")
  303. assert(string.format("%+#014.0f", 100) == "+000000000100.")
  304. assert(string.format("% 1.0E", 100) == " 1E+02")
  305. assert(string.format("%-16c", 97) == "a ")
  306. assert(string.format("%+.3G", 1.5) == "+1.5")
  307. assert(string.format("% .1g", 2^10) == " 1e+03")
  308. assert(string.format("%.0s", "alo") == "")
  309. assert(string.format("%.s", "alo") == "")
  310. -- errors in format
  311. local function check (fmt, msg)
  312. checkerror(msg, string.format, fmt, 10)
  313. end
  314. local aux = string.rep('0', 600)
  315. check("%100.3d", "invalid conversion")
  316. check("%1"..aux..".3d", "too long")
  317. check("%1.100d", "invalid conversion")
  318. check("%10.1"..aux.."004d", "too long")
  319. check("%t", "invalid conversion")
  320. check("%"..aux.."d", "too long")
  321. check("%d %d", "no value")
  322. check("%010c", "invalid conversion")
  323. check("%.10c", "invalid conversion")
  324. check("%0.34s", "invalid conversion")
  325. check("%#i", "invalid conversion")
  326. check("%3.1p", "invalid conversion")
  327. check("%0.s", "invalid conversion")
  328. check("%10q", "cannot have modifiers")
  329. check("%F", "invalid conversion") -- useless and not in C89
  330. assert(load("return 1\n--comment without ending EOL")() == 1)
  331. checkerror("table expected", table.concat, 3)
  332. checkerror("at index " .. maxi, table.concat, {}, " ", maxi, maxi)
  333. -- '%' escapes following minus signal
  334. checkerror("at index %" .. mini, table.concat, {}, " ", mini, mini)
  335. assert(table.concat{} == "")
  336. assert(table.concat({}, 'x') == "")
  337. assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2")
  338. local a = {}; for i=1,300 do a[i] = "xuxu" end
  339. assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300))
  340. assert(table.concat(a, "b", 20, 20) == "xuxu")
  341. assert(table.concat(a, "", 20, 21) == "xuxuxuxu")
  342. assert(table.concat(a, "x", 22, 21) == "")
  343. assert(table.concat(a, "3", 299) == "xuxu3xuxu")
  344. assert(table.concat({}, "x", maxi, maxi - 1) == "")
  345. assert(table.concat({}, "x", mini + 1, mini) == "")
  346. assert(table.concat({}, "x", maxi, mini) == "")
  347. assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo")
  348. assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi)
  349. == "y-alo")
  350. assert(not pcall(table.concat, {"a", "b", {}}))
  351. a = {"a","b","c"}
  352. assert(table.concat(a, ",", 1, 0) == "")
  353. assert(table.concat(a, ",", 1, 1) == "a")
  354. assert(table.concat(a, ",", 1, 2) == "a,b")
  355. assert(table.concat(a, ",", 2) == "b,c")
  356. assert(table.concat(a, ",", 3) == "c")
  357. assert(table.concat(a, ",", 4) == "")
  358. if not _port then
  359. local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" }
  360. local function trylocale (w)
  361. for i = 1, #locales do
  362. if os.setlocale(locales[i], w) then
  363. print(string.format("'%s' locale set to '%s'", w, locales[i]))
  364. return locales[i]
  365. end
  366. end
  367. print(string.format("'%s' locale not found", w))
  368. return false
  369. end
  370. if trylocale("collate") then
  371. assert("alo" < "álo" and "álo" < "amo")
  372. end
  373. if trylocale("ctype") then
  374. assert(string.gsub("áéíóú", "%a", "x") == "xxxxx")
  375. assert(string.gsub("áÁéÉ", "%l", "x") == "xÁxÉ")
  376. assert(string.gsub("áÁéÉ", "%u", "x") == "áxéx")
  377. assert(string.upper"áÁé{xuxu}ção" == "ÁÁÉ{XUXU}ÇÃO")
  378. end
  379. os.setlocale("C")
  380. assert(os.setlocale() == 'C')
  381. assert(os.setlocale(nil, "numeric") == 'C')
  382. end
  383. -- bug in Lua 5.3.2
  384. -- 'gmatch' iterator does not work across coroutines
  385. do
  386. local f = string.gmatch("1 2 3 4 5", "%d+")
  387. assert(f() == "1")
  388. co = coroutine.wrap(f)
  389. assert(co() == "2")
  390. end
  391. if T==nil then
  392. (Message or print)
  393. ("\n >>> testC not active: skipping 'pushfstring' tests <<<\n")
  394. else
  395. print"testing 'pushfstring'"
  396. -- formats %U, %f, %I already tested elsewhere
  397. local blen = 200 -- internal buffer length in 'luaO_pushfstring'
  398. local function callpfs (op, fmt, n)
  399. local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)}
  400. -- stack has code, 'fmt', 'n', and result from operation
  401. assert(#x == 4) -- make sure nothing else was left in the stack
  402. return x[4]
  403. end
  404. local function testpfs (op, fmt, n)
  405. assert(callpfs(op, fmt, n) == string.format(fmt, n))
  406. end
  407. testpfs("I", "", 0)
  408. testpfs("I", string.rep("a", blen - 1), 0)
  409. testpfs("I", string.rep("a", blen), 0)
  410. testpfs("I", string.rep("a", blen + 1), 0)
  411. local str = string.rep("ab", blen) .. "%d" .. string.rep("d", blen / 2)
  412. testpfs("I", str, 2^14)
  413. testpfs("I", str, -2^15)
  414. str = "%d" .. string.rep("cd", blen)
  415. testpfs("I", str, 2^14)
  416. testpfs("I", str, -2^15)
  417. str = string.rep("c", blen - 2) .. "%d"
  418. testpfs("I", str, 2^14)
  419. testpfs("I", str, -2^15)
  420. for l = 12, 14 do
  421. local str1 = string.rep("a", l)
  422. for i = 0, 500, 13 do
  423. for j = 0, 500, 13 do
  424. str = string.rep("a", i) .. "%s" .. string.rep("d", j)
  425. testpfs("S", str, str1)
  426. testpfs("S", str, str)
  427. end
  428. end
  429. end
  430. str = "abc %c def"
  431. testpfs("I", str, string.byte("A"))
  432. testpfs("I", str, 255)
  433. str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen)
  434. testpfs("P", str, {})
  435. str = string.rep("%%", 3 * blen) .. "%p" .. string.rep("%%", 2 * blen)
  436. testpfs("P", str, {})
  437. end
  438. print('OK')