View Single Post
09/09/18, 06:27 AM   #14
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,578
I just did my own tests and the string functions don't count unicode characters as others have suggested.

I tested it with the string "これは日本語の文字列です" with string.len, the # operator, two methods proposed on the lua-users wiki and the utf8len function in this gist.
Lua Code:
  1. local input = "これは日本語の文字列です"
  2. local count1 = input:len()
  3. local count2 = #input
  4. local _, count3 = string.gsub(input, "[^\128-\193]","")
  5. local count4 = 0
  6. for uchar in string.gmatch(input, "([%z\1-\127\194-\244][\128-\191]*)") do
  7.     count4 = count4 + 1
  8. end
  9. local count5 = utf8len(input)
  10. d(count1, count2, count3, count4, count5)
The output is 36, 36, 36, 0, 12, meaning that the string functions use byte count, gsub and gmatch do something unexpected and the only way right now is to use some library to count how many characters you have left.

As for your original problem, you can always use a function that manages saving and reading the saved string and split it up dynamically. Here is an example that I just slapped together:

Lua Code:
  1. local MAX_SAVE_DATA_LENGTH = 2000 -- buffer length used by ZOS
  2. local function WriteLongString(obj, key, value)
  3.     local output = value
  4.     local byteLength = #value
  5.     if(byteLength > MAX_SAVE_DATA_LENGTH) then
  6.         output = {}
  7.         local startPos = 1
  8.         local endPos = startPos + MAX_SAVE_DATA_LENGTH - 1
  9.         while startPos <= byteLength do
  10.             output[#output + 1] = string.sub(value, startPos, endPos)
  11.             startPos = endPos + 1
  12.             endPos = startPos + MAX_SAVE_DATA_LENGTH - 1
  13.         end
  14.     end
  15.     obj[key] = output
  16. end
  17.  
  18. local function ReadLongString(obj, key)
  19.     local value = obj[key]
  20.     if(type(value) == "table") then
  21.         return table.concat(value, "")
  22.     end
  23.     return value
  24. end