-
Notifications
You must be signed in to change notification settings - Fork 90
Feature: implemented all utf8 functions in stdlib
#110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
… `utf8.len`, `utf8.codepoint` and `utf8.offset`
|
Note that Lua 5.4 has different behavior from Lua 5.3 for the For more info, see the Lua 5.4 docs on the utf8 module. The main question is whether piccolo should even support the lax mode, since if we don't, it simplifies the implementation a lot; most parts could be implemented with byte slices and |
|
For the Here's a description of the iterator/generic for loop approach, from the Lua 5.3 docs (but it's roughly the same in 5.4).
The standard implementation of |
|
I've simplified |
|
Thank you! I agree with not supporting lax mode, since it makes everything simpler. If it helps with the indexing math, I'm pretty sure fn convert_index(i: i64, len: usize) -> Option<usize> {
let val = match i {
0 => 0,
v @ 1.. => v - 1,
v @ ..=-1 => (len as i64 + v).max(0),
};
usize::try_from(val).ok()
}
fn convert_index_end(i: i64, len: usize) -> Option<usize> {
let val = match i {
v @ 0.. => v,
v @ ..=-1 => (len as i64 + v + 1).max(0),
};
usize::try_from(val).ok()
}
fn sub(string: &[u8], i: i64, j: i64) -> &[u8] {
let len = string.len();
let i = convert_index(i, len).unwrap_or(usize::MAX);
let j = convert_index_end(j, len).unwrap_or(usize::MAX).min(len);
let slice = if i > j { &[] } else { &string[i..j] };
slice
}IIRC the above code worked when I tested it, and should simplify Here are a few other ideas for ways of simplifying it:
Also, the |
|
I've simplified |
|
For I'm not really sure if there's a clean way to improve
Given that the docs say
I think the current approach of scanning for the continuation bytes is fine. |
src/stdlib/utf8.rs
Outdated
| if c.is_ascii() { | ||
| stack.replace(ctx, (n as i64 + 1, c as i64)); | ||
| } else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check isn't needed; len_utf8 will return 1 if c is ascii.
|
Hi! I have implemented all of your recommendations. May we proceed with the integration? |
All functions are based on and fully comply with the Lua 5.3 manual (https://www.lua.org/manual/5.3/).
Features
utf8.char,utf8.charpattern(string),utf8.codes,utf8.codepoint,utf8.len,utf8.offset.utf8.luafor all new functions.