-
Notifications
You must be signed in to change notification settings - Fork 61
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
Strings Library #12
Strings Library #12
Conversation
features like |
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.
Lots of suggestions about conveying information. Take it as you see fit. If any of my suggestions seem like an improvement then I suggest to not just copy and paste them in since I have put some thought into them but more careful wording may be selected.
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.
Can we use the string library with chars such as 'a' ? If so I would like to see some tests using characters instead of numbers
sway_libs/src/string/README.md
Outdated
|
||
For more information please see the [specification](./SPECIFICATION.md). | ||
|
||
> **Note** There is currently no way to convert a `str` to a `String`. |
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.
If a str cannot be iterated over than I would agree with this note as Braqzen has noted as well. Did you mean perhaps to say an str cannot be casted to a String? IMO you should be able to do let s1: String = String::from("this is an str");
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 don't believe this will be possible until type constraints are implemented. I think that is part of the declaration engine which afaik @emilyaherbert and @tritao are working on?
Character in Sway do not exist. There is an issue that has been proposed here |
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.
Perhaps we only want to start off with the interface, but then we should state that this does no validation and essentially is just Vec<u8>
(which Rust's String
is also backed by, but the API is unicode-aware).
sway_libs/src/string/string.sw
Outdated
/// Converts a vector of bytes to a `String`. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `bytes` - The vector of `u8` bytes which will be converted into a `String`. | ||
pub fn from_utf8(bytes: Vec<u8>) -> String { | ||
String { bytes } | ||
} | ||
|
||
/// Inserts a byte at the index within the `String`. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `byte` - The element which will be added to the `String`. | ||
/// * `index` - The position in the `String` where the element will be inserted. | ||
pub fn insert(self, byte: u8, index: u64) { | ||
self.bytes.insert(index, 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.
These functions, as well as push
, do not seem to be "sound" with respect to the stated purpose of String
:
- String is used for dynamic length strings that are UTF-8 encoded.
To start with, from_utf8
does no validation that the byte array actually is valid UTF-8. It might not be.
Then, let's assume that your string is valid. This can be broken by insert
ting bad bytes. Similarly, you can push
the bad bytes onto the hitherto valid string.
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.
Yes absolutely this is correct, my documentation is definitely misinforming in regards to this.
sway_libs/src/string/string.sw
Outdated
/// Removes the last character from the `String` buffer and returns it. | ||
pub fn pop(self) -> Option<u8> { | ||
self.bytes.pop() | ||
} |
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 and remove
are problematic as well in terms of unicode.
As the use for strings in blockchains is pretty minimal and with the current support for characters and Nonetheless, I do hope to incorporate a safer use of strings once chars are available. |
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 with the new docs 🚀
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.
lgtm
|
||
For more information please see the [specification](./SPECIFICATION.md). | ||
|
||
> **Note** There is currently no way to convert a `str` to a `String`. | ||
|
||
## Known Issues | ||
|
||
It is important to note that unlike Rust's `String`, this `String` library does **not** guarantee a valid UTF-8 string. The `String` currently behaves only as a `vec` and does not perform any validation. This intended to be supported in the future with the introduction of [`char`](https://github.com/FuelLabs/sway/issues/2937) to the Sway language. |
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.
Good comment, this clears up a lot of the confusion I was having with the lib
Type of change
Changes
The following changes have been made:
Notes
Vec
in the standard library and not with the string stuct.str
andString
. This is due to the compiler's handling ofstr
.Related Issues
Closes #11