String:sub(i,j) failing with Unicode

, ,

I have a text file with some unicode arrows in it.

←→↑↓

I also have a local string with the same characters.

I can read these, print them and assert that the entire string is correct:

local pd <const> = playdate

local fileHandle <const> = pd.file.open('arrows.txt', pd.file.kFileRead)

local row = fileHandle:readline()

print('Row', row)
assert(row ==  "←→↑↓")

However, when I try to match a single character the entire line is skipped, i.e. it's not even executed.

Here's the entire test code:

local pd <const> = playdate

local fileHandle <const> = pd.file.open('arrows.txt', pd.file.kFileRead)

local row = fileHandle:readline()

print('1. Row', row)
assert(row ==  "←→↑↓")
print('2. Individual row chars', row:sub(1, 1), row:sub(2, 2), row:sub(3, 3), row:sub(4, 4))

local arrowString = '←→↑↓'
print('3. Local string', arrowString)
print('4. Individual local characters', arrowString:sub(1, 1), arrowString:sub(2, 2), arrowString:sub(3, 3), arrowString:sub(4, 4))
print("5. '←' == '←'", '←' == '←')

print("6. row:sub(1,1)", row:sub(1,1))
print("7. row:sub(1,1) == '←'", row:sub(1,1) == '←')
print("8. arrowString:sub(1,1)", arrowString:sub(1,1))
print("9. arrowString:sub(1,1) == '←'", arrowString:sub(1,1) == '←')

function pd.update()
end

The console log for this is:

1. Row	←→↑↓
3. Local string	←→↑↓
5. '←' == '←'	true
7. row:sub(1,1) == '←'	false
9. arrowString:sub(1,1) == '←'	false

This looks like it's not a bug in the PlayDate SDK.

Thanks to rkjr2 on Reddit.

It seems that this is a feature of Lua. Lua handles Unicode with all the grace that a whippet handles a squirrel.

The Lua documentation says that you need to use a special set of functions for handling unicode characters. It looks like a bit of a nightmare.

1 Like