Skip to content
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

Remove boa_macros from boa_string #3913

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions core/string/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ static_assertions.workspace = true
paste.workspace = true
fast-float.workspace = true

[dev-dependencies]
boa_macros.workspace = true

[lints]
workspace = true

Expand Down
27 changes: 18 additions & 9 deletions core/string/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ use std::hash::{BuildHasher, BuildHasherDefault, Hash};

use crate::{JsStr, JsString, StaticJsStrings};

use boa_macros::utf16;
use rustc_hash::FxHasher;

fn hash_value<T: Hash>(value: &T) -> u64 {
BuildHasherDefault::<FxHasher>::default().hash_one(value)
}

const fn ascii_to_utf16<const LEN: usize>(ascii: &[u8; LEN]) -> [u16; LEN] {
let mut array = [0; LEN];
let mut i = 0;
while i < LEN {
array[i] = ascii[i] as u16;
i += 1;
}
array
}

#[test]
fn empty() {
let s = StaticJsStrings::EMPTY_STRING;
assert_eq!(&s, utf16!(""));
assert_eq!(&s, &[]);
}

#[test]
Expand Down Expand Up @@ -85,7 +94,7 @@ fn static_ptr_eq() {

#[test]
fn as_str() {
const HELLO: &[u16] = utf16!("Hello");
const HELLO: &[u16] = &ascii_to_utf16(b"Hello");
let x = JsString::from(HELLO);

assert_eq!(&x, HELLO);
Expand All @@ -109,22 +118,22 @@ fn hash() {

#[test]
fn concat() {
const Y: &[u16] = utf16!(", ");
const W: &[u16] = utf16!("!");
const Y: &[u16] = &ascii_to_utf16(b", ");
const W: &[u16] = &ascii_to_utf16(b"!");

let x = JsString::from("hello");
let z = JsString::from("world");

let xy = JsString::concat(x.as_str(), JsString::from(Y).as_str());
assert_eq!(&xy, utf16!("hello, "));
assert_eq!(&xy, &ascii_to_utf16(b"hello, "));
assert_eq!(xy.refcount(), Some(1));

let xyz = JsString::concat(xy.as_str(), z.as_str());
assert_eq!(&xyz, utf16!("hello, world"));
assert_eq!(&xyz, &ascii_to_utf16(b"hello, world"));
assert_eq!(xyz.refcount(), Some(1));

let xyzw = JsString::concat(xyz.as_str(), JsString::from(W).as_str());
assert_eq!(&xyzw, utf16!("hello, world!"));
assert_eq!(&xyzw, &ascii_to_utf16(b"hello, world!"));
assert_eq!(xyzw.refcount(), Some(1));
}

Expand All @@ -141,7 +150,7 @@ fn trim_start_non_ascii_to_ascii() {
#[test]
fn conversion_to_known_static_js_string() {
const JS_STR_U8: &JsStr<'_> = &JsStr::latin1("length".as_bytes());
const JS_STR_U16: &JsStr<'_> = &JsStr::utf16(utf16!("length"));
const JS_STR_U16: &JsStr<'_> = &JsStr::utf16(&ascii_to_utf16(b"length"));

assert!(JS_STR_U8.is_latin1());
assert!(!JS_STR_U16.is_latin1());
Expand Down
Loading