-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[framework] Implements ascii + utf8 strings all over again #18462
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
3 Ignored Deployments
|
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.
Looks good!
const EINVALID_ASCII_CHARACTER: u64 = 0x10000; | ||
const EInvalidASCIICharacter: u64 = 0x10000; | ||
/// An invalid index was encountered when creating a substring. | ||
const EInvalidIndex: u64 = 0x10001; |
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.
ughhhh but fine this is the right thing to do
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.
Idk I think we should at some point switch all of the std/sui errors to using clever errors.
It will be a pain, and maybe break someone somewhere, but I feel like it is the right thing to do long term
const EINVALID_ASCII_CHARACTER: u64 = 0x10000; | ||
const EInvalidASCIICharacter: u64 = 0x10000; | ||
/// An invalid index was encountered when creating a substring. | ||
const EInvalidIndex: u64 = 0x10001; |
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.
Idk I think we should at some point switch all of the std/sui errors to using clever errors.
It will be a pain, and maybe break someone somewhere, but I feel like it is the right thing to do long term
i = i + 1; | ||
}; | ||
option::some(String { bytes }) | ||
let is_valid = bytes.all!(|byte| is_valid_char(*byte)); |
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.
🔥
i = i + 1; | ||
}; | ||
true | ||
string.bytes.all!(|byte| is_printable_char(*byte)) |
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.
🔥 🔥
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.
/// Insert the `other` string at the `at` index of `string`. | ||
public fun insert(s: &mut String, at: u64, o: String) { | ||
assert!(at <= s.length(), EInvalidIndex); | ||
o.into_bytes().destroy!(|e| s.bytes.insert(e, at)); |
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.
🔥 🔥 🔥
let mut bytes = vector[]; | ||
i.range_do!(j, |i| bytes.push_back(string.bytes[i])); |
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.
I might prefer vector::tabulate
for this one. But this one is good too :)
let bytes = string.as_bytes().map_ref!(|byte| char_to_uppercase(*byte)); | ||
String { bytes } | ||
} | ||
|
||
/// Convert a `string` to its lowercase equivalent. | ||
public fun to_lowercase(string: &String): String { | ||
let bytes = string.as_bytes().map_ref!(|byte| char_to_lowercase(*byte)); | ||
String { bytes } |
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.
🔥 🔥 🔥 🔥
/// Returns the length of the `string` if the `substr` is not found. | ||
/// Returns 0 if the `substr` is empty. |
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.
I really hate this API, but I agree it is the right thing to do for consistency
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.
Same here, not a fan.
/// [DEPRECATED] | ||
public fun bytes(s: &String): &vector<u8> { s.as_bytes() } | ||
|
||
/// [DEPRECATED] | ||
public fun sub_string(s: &String, i: u64, j: u64): String { | ||
s.substring(i, j) | ||
} |
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.
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.
@damirka #[deprecated]
should land soon ™️ , if it hasn't already
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.
Literally just landed!
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.
And.... it's gone
…s#18462) ## Description This PR rolls MystenLabs#17380 (successfully merged once) yet again. ## Test plan Features tests. ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates. - [x] Protocol: This change updates the [ascii module](https://docs.sui.io/references/framework/move-stdlib/ascii) in the following ways: Adds new methods to `std::ascii`: - `ascii::append(&mut String, String)` - `ascii::is_empty(): bool` - `ascii::substring(&String, i, j): String` - `ascii::index_of(&String, &String): u64` - `ascii::to_uppercase(&String): String` - `ascii::to_lowercase(&String): String` These additions make the ASCII interface more similar to the UTF8 one. Renames: - `string::bytes() to string::as_bytes()` - `string::sub_string() to string::substring()` Deprecates: - `string::sub_string` in favour of `string::substring` - `string::bytes` in favour of `string::as_bytes` Additional changes: - updates `std::type_name` to use `std::substring` - removes use statements for implicit imports - renames constants from `E_INDEX` to conventional `EIndexOutOfBounds`
Description
This PR rolls #17380 (successfully merged once) yet again.
Test plan
Features tests.
Release notes
Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required.
For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates.
Protocol: This change updates the ascii module in the following ways:
std::ascii
:ascii::append(&mut String, String)
ascii::is_empty(): bool
ascii::substring(&String, i, j): String
ascii::index_of(&String, &String): u64
ascii::to_uppercase(&String): String
ascii::to_lowercase(&String): String
These additions make the ASCII interface more similar to the UTF8 one.
Renames:
-
string::bytes() to string::as_bytes()
-
string::sub_string() to string::substring()
Deprecates:
-
string::sub_string
in favour ofstring::substring
-
string::bytes
in favour ofstring::as_bytes
Additional changes:
- updates
std::type_name
to usestd::substring
- removes use statements for implicit imports
- renames constants from
E_INDEX
to conventionalEIndexOutOfBounds