Skip to content

Commit

Permalink
feat(stdlib): Implement str as_bytes and into_bytes function (#…
Browse files Browse the repository at this point in the history
…2298)

Co-authored-by: jfecher <[email protected]>
  • Loading branch information
Ethan-000 and jfecher authored Aug 15, 2023
1 parent a7bb40b commit 92549d4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "str_as_bytes"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
18 changes: 18 additions & 0 deletions crates/nargo_cli/tests/execution_success/str_as_bytes/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use dep::std;
fn main() {
let a = "hello";
let b = a.as_bytes();
assert(b[0]==104);
assert(b[1]==101);
assert(b[2]==108);
assert(b[3]==108);
assert(b[4]==111);
assert(b.len()==5);
let mut c = a.as_bytes_vec();
assert(c.get(0)==104);
assert(c.get(1)==101);
assert(c.get(2)==108);
assert(c.get(3)==108);
assert(c.get(4)==111);
assert(c.len()==5);
}
3 changes: 3 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub(crate) enum Intrinsic {
SlicePopFront,
SliceInsert,
SliceRemove,
StrAsBytes,
Println,
ToBits(Endian),
ToRadix(Endian),
Expand All @@ -60,6 +61,7 @@ impl std::fmt::Display for Intrinsic {
Intrinsic::SlicePopFront => write!(f, "slice_pop_front"),
Intrinsic::SliceInsert => write!(f, "slice_insert"),
Intrinsic::SliceRemove => write!(f, "slice_remove"),
Intrinsic::StrAsBytes => write!(f, "str_as_bytes"),
Intrinsic::ToBits(Endian::Big) => write!(f, "to_be_bits"),
Intrinsic::ToBits(Endian::Little) => write!(f, "to_le_bits"),
Intrinsic::ToRadix(Endian::Big) => write!(f, "to_be_radix"),
Expand All @@ -84,6 +86,7 @@ impl Intrinsic {
"slice_pop_front" => Some(Intrinsic::SlicePopFront),
"slice_insert" => Some(Intrinsic::SliceInsert),
"slice_remove" => Some(Intrinsic::SliceRemove),
"str_as_bytes" => Some(Intrinsic::StrAsBytes),
"to_le_radix" => Some(Intrinsic::ToRadix(Endian::Little)),
"to_be_radix" => Some(Intrinsic::ToRadix(Endian::Big)),
"to_le_bits" => Some(Intrinsic::ToBits(Endian::Little)),
Expand Down
4 changes: 4 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ pub(super) fn simplify_call(
SimplifyResult::None
}
}
Intrinsic::StrAsBytes => {
// Strings are already represented as bytes internally
SimplifyResult::SimplifiedTo(arguments[0])
}
Intrinsic::AssertConstant => {
if arguments.iter().all(|argument| dfg.is_constant(*argument)) {
SimplifyResult::Remove
Expand Down
1 change: 1 addition & 0 deletions noir_stdlib/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "std"
type = "lib"
authors = [""]
compiler_version = "0.1"

Expand Down
1 change: 1 addition & 0 deletions noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod unsafe;
mod collections;
mod compat;
mod option;
mod string;

// Oracle calls are required to be wrapped in an unconstrained function
// Thus, the only argument to the `println` oracle is expected to always be an ident
Expand Down
11 changes: 11 additions & 0 deletions noir_stdlib/src/string.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::collections::vec::Vec;
impl<N> str<N> {
/// Converts the given string into a byte array
#[builtin(str_as_bytes)]
fn as_bytes(_self: Self) -> [u8; N] { }

/// return a byte vector of the str content
fn as_bytes_vec(self: Self) -> Vec<u8> {
Vec::from_slice(self.as_bytes().as_slice())
}
}

0 comments on commit 92549d4

Please sign in to comment.