-
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
Merged
Merged
Strings Library #12
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
c1f05ee
Inital Commit
1dbabcc
Add base functions
8a24ca7
Refactor and Rename to match Rust
80471e3
Tests outline
2838a0d
Update to string
de3d3bf
Add len function test
4b338c7
Add is_empty tests
e16aeb9
Add clear function tests
6caec29
Add capacity function tests
b109dfb
Add new function tests
e3ab49c
Add push function tests
352c997
Fix capacity test
2830e6b
Add with_capacity function tests
cf8608f
Add some more functions
f3f195b
First iteration of push_str
3888a91
Resolve Merge conflicts
eafb587
Remove functions with relation to str
d7bf98d
Update function test list to match new string methods
c37c295
Update empty tests
0529954
Update len tests
5033649
Update push test
b2089bf
Update to use constants for numbers
83ce059
Add as_bytes test
54406dc
Add from_utf8 test
ab7f0a1
Add insert test
31dfbeb
Add nth test
bd7f72d
Add test for pop
11ea781
Add remove test
73c397b
Update rust SDK for tests
3c9df9f
Run formatter
2db5115
Fix bizarre formatting
2863cd5
Update inline documentation
d003e41
Update formatting
63880ab
Remove Forc.lock
00a417b
Add README.md and SPECIFICATION.md
83b5602
Fix README link
eb02cc9
Merge remote-tracking branch 'origin/master' into bitzoic-11
68afba3
Add how to import to README
e128a7f
Update code based on suggestions
d3b5006
Update documentation based on suggestions
0080b06
Remove warnings and fix instance calls
33a00f7
Merge remote-tracking branch 'origin/master' into bitzoic-11
b7e61fd
Update wording of documentation to inform of String's usage of unsafe…
7db8761
Update incorrect link to source README
ae7a296
Refactor SDK tests into multiple files
89d06d6
Add None for nth test
c79457c
Add EOD to test file
446f6a7
Update Documentation based on suggestions
ab46610
Remove unneeded braces from tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
library sway_libs; | ||
|
||
dep merkle_proof/binary_merkle_proof; | ||
dep string/string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Overview | ||
|
||
The String library provides an interface to use UTF-8 encoded strings of dynamic length in Sway. The `String` is heap allocated, growable, and not null terminated. | ||
|
||
The `String` is stored as vector of bytes. This differs from Sway's built in `str` because the size cannot be known at compile time and the length is dynamic. | ||
|
||
For more information please see the [specification](./SPECIFICATION.md). | ||
|
||
> **Note** There is 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. | ||
|
||
# Using the Library | ||
|
||
## Getting Started | ||
|
||
In order to use the `String` library it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [README.md](../../../README.md). | ||
|
||
```rust | ||
use sway_libs::string::String; | ||
``` | ||
|
||
Once imported, a `String` can be instantiated defining a new variable and calling the `new` function. | ||
|
||
```rust | ||
let mut string = ~String::new(); | ||
``` | ||
|
||
## Basic Functionality | ||
|
||
Appending or adding bytes to the `String` can be done by calling the `push` and `insert` functions. | ||
|
||
```rust | ||
// Append to the end | ||
string.push(0u8); | ||
|
||
// Insert at index 0 | ||
string.insert(0u8, 0); | ||
``` | ||
|
||
Removing bytes from the `String` can be done with either the `pop` or `remove` functions. | ||
|
||
```rust | ||
// Remove the last byte from the string, return the option that wraps the value and unwrap the byte | ||
let last_byte = string.pop().unwrap(); | ||
|
||
// Remove and return the byte at index 0 | ||
let nth_byte = string.remove(0); | ||
``` | ||
|
||
To retrieve a byte in the `String`, use the `nth` function. | ||
|
||
```rust | ||
// Retrieve the byte at index 0 | ||
let nth_byte = string.nth(0); | ||
bitzoic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
For more information please see the [specification](./SPECIFICATION.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Overview | ||
bitzoic marked this conversation as resolved.
Show resolved
Hide resolved
matt-user marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This document provides an overview of the String library. | ||
|
||
It outlines the use cases, i.e. specification, and describes how to implement the library. | ||
|
||
## Use Cases | ||
|
||
The String library can be used anytime a string's length is unknown at compile time. Further methods can then be implemented to provide additional features building off of the `String` struct. | ||
|
||
> **Note** There is no guarantee in the validity of the UTF-8 encoded `String` and should be used with caution. For more information, please see the [known issues](./README.md#known-issues). | ||
|
||
## Public Functions | ||
|
||
### `as_bytes()` | ||
|
||
Convert the `String` struct to a `Vec` of `u8` bytes. | ||
|
||
### `capacity()` | ||
|
||
Returns the total amount of memory on the heap allocated to the `String` which can be filled with bytes. | ||
|
||
> **Note** Capacity and length are not the same. A `String` may have a length of 0 but any positive capacity. | ||
|
||
### `clear()` | ||
|
||
Truncates the `String` to a length of 0 and will appear empty. This does not clear the capacity of the `String`. | ||
|
||
### `from_utf8()` | ||
|
||
A new instance of a `String` will be created from a vector of `u8`'s | ||
|
||
### `insert()` | ||
|
||
Inserts a new byte at the specified index in the `String`. | ||
|
||
### `is_empty()` | ||
|
||
Returns a boolean indicating whether the length of the `String` is zero. | ||
|
||
### `len()` | ||
|
||
Returns the total number of bytes in the `String`. | ||
|
||
### `new()` | ||
|
||
Creates a new instance of the `String` struct. | ||
|
||
### `nth()` | ||
|
||
Returns the byte at the specified index in the `String`. If the index is out of bounds, `None` is returned. | ||
|
||
### `pop()` | ||
|
||
Removes the last byte in the `String` and returns it. If the `String` does not have any bytes, `None` is returned. | ||
|
||
### `remove()` | ||
|
||
Remove and return the specified byte in the `String`. | ||
|
||
### `with_capacity()` | ||
|
||
Creates a new instance of the `String` struct with a specified amount of memory allocated on the heap. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
library string; | ||
Braqzen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use std::{option::Option, vec::Vec}; | ||
Braqzen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pub struct String { | ||
bytes: Vec<u8>, | ||
} | ||
|
||
impl String { | ||
/// Returns the bytes stored for the `String`. | ||
pub fn as_bytes(self) -> Vec<u8> { | ||
self.bytes | ||
} | ||
|
||
/// Gets the amount of memory on the heap allocated to the `String`. | ||
pub fn capacity(self) -> u64 { | ||
self.bytes.capacity() | ||
} | ||
|
||
/// Truncates this `String` to a length of zero, removing all contents. | ||
pub fn clear(self) { | ||
self.bytes.clear() | ||
} | ||
|
||
/// 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 byte will be inserted. | ||
pub fn insert(self, byte: u8, index: u64) { | ||
self.bytes.insert(index, byte); | ||
} | ||
|
||
/// Returns `true` if the vector contains no bytes. | ||
pub fn is_empty(self) -> bool { | ||
self.bytes.is_empty() | ||
} | ||
|
||
/// Returns the number of bytes in the `String`, also referred to | ||
/// as its 'length'. | ||
pub fn len(self) -> u64 { | ||
self.bytes.len() | ||
} | ||
|
||
/// Constructs a new instance of the `String` type. | ||
pub fn new() -> Self { | ||
Self { | ||
bytes: ~Vec::new(), | ||
} | ||
} | ||
|
||
/// Returns the byte at the specified index. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `index` - The position of the byte that will be returned. | ||
pub fn nth(self, index: u64) -> Option<u8> { | ||
self.bytes.get(index) | ||
} | ||
|
||
/// Removes the last byte from the `String` buffer and returns it. | ||
pub fn pop(self) -> Option<u8> { | ||
self.bytes.pop() | ||
} | ||
|
||
/// Appends a byte to the end of the `String`. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `byte` - The element to be appended to the end of the `String`. | ||
pub fn push(self, byte: u8) { | ||
self.bytes.push(byte); | ||
} | ||
|
||
/// Removes and returns the byte at the specified index. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `index` - The position of the byte that will be removed. | ||
pub fn remove(self, index: u64) -> u8 { | ||
self.bytes.remove(index) | ||
} | ||
|
||
/// Constructs a new instance of the `String` type with the specified capacity. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `capacity` - The specified amount of memory on the heap to be allocated for the `String`. | ||
pub fn with_capacity(capacity: u64) -> Self { | ||
Self { | ||
bytes: ~Vec::with_capacity(capacity), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// Add test modules here: | ||
|
||
mod merkle_proof; | ||
mod string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "string" | ||
|
||
[dependencies] | ||
sway_libs = { path = "../../../../sway_libs" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod tests; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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