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

Use cow-utils instead #4133

Merged
merged 4 commits into from
Jan 18, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Use cow-utils instead
heygsc committed Jan 14, 2025
commit b73c9f3f4a1f0df777d75ed73bf66912317dd02d
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ phf = { workspace = true, features = ["macros"] }
pollster.workspace = true
dhat = { workspace = true, optional = true }
color-eyre.workspace = true
cow-utils.workspace = true

[features]
default = ["boa_engine/annex-b", "boa_engine/experimental", "boa_engine/intl_bundled"]
5 changes: 3 additions & 2 deletions cli/src/debug/function.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ use boa_engine::{
vm::flowgraph::{Direction, Graph},
Context, JsArgs, JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
};
use cow_utils::CowUtils;

use crate::FlowgraphFormat;

@@ -14,7 +15,7 @@ fn flowgraph_parse_format_option(value: &JsValue) -> JsResult<FlowgraphFormat> {
}

if let Some(string) = value.as_string() {
return match string.to_std_string_escaped().to_lowercase().as_str() {
return match string.to_std_string_escaped().cow_to_lowercase().as_ref() {
"mermaid" => Ok(FlowgraphFormat::Mermaid),
"graphviz" => Ok(FlowgraphFormat::Graphviz),
format => Err(JsNativeError::typ()
@@ -34,7 +35,7 @@ fn flowgraph_parse_direction_option(value: &JsValue) -> JsResult<Direction> {
}

if let Some(string) = value.as_string() {
return match string.to_std_string_escaped().to_lowercase().as_str() {
return match string.to_std_string_escaped().cow_to_lowercase().as_ref() {
"leftright" | "lr" => Ok(Direction::LeftToRight),
"rightleft" | "rl" => Ok(Direction::RightToLeft),
"topbottom" | "tb" => Ok(Direction::TopToBottom),
5 changes: 5 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
doc-valid-idents = ['ECMAScript', 'JavaScript', 'SpiderMonkey', 'GitHub']
allow-print-in-tests = true
disallowed-methods = [
{ path = "str::to_ascii_lowercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_ascii_lowercase` instead." },
{ path = "str::to_ascii_uppercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_ascii_uppercase` instead." },
{ path = "str::to_lowercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_lowercase` instead." },
{ path = "str::to_uppercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_uppercase` instead." },
{ path = "str::replace", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_replace` instead." },
{ path = "str::replacen", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_replacen` instead." },
]
5 changes: 3 additions & 2 deletions core/engine/src/builtins/number/globals.rs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ use crate::{
};

use boa_macros::js_str;
use cow_utils::CowUtils;

/// Builtin javascript 'isFinite(number)' function.
///
@@ -304,8 +305,8 @@ pub(crate) fn parse_float(
// TODO: parse float with optimal utf16 algorithm
let input_string = val.to_string(context)?.to_std_string_escaped();
let s = input_string.trim_start_matches(is_trimmable_whitespace);
let s_prefix_lower = s.chars().take(4).collect::<String>().to_ascii_lowercase();

let s_prefix = s.chars().take(4).collect::<String>();
let s_prefix_lower = s_prefix.cow_to_ascii_lowercase();
// TODO: write our own lexer to match syntax StrDecimalLiteral
if s.starts_with("Infinity") || s.starts_with("+Infinity") {
Ok(JsValue::new(f64::INFINITY))
3 changes: 2 additions & 1 deletion core/engine/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ use crate::{
Context, JsArgs, JsResult, JsString,
};
use boa_profiler::Profiler;
use cow_utils::CowUtils;
use num_traits::float::FloatCore;

mod globals;
@@ -916,7 +917,7 @@ impl Number {
/// Helper function that formats a float as a ES6-style exponential number string.
fn f64_to_exponential(n: f64) -> JsString {
js_string!(match n.abs() {
x if x >= 1.0 || x == 0.0 => format!("{n:e}").replace('e', "e+"),
x if x >= 1.0 || x == 0.0 => format!("{n:e}").cow_replace('e', "e+").to_string(),
_ => format!("{n:e}"),
})
}
5 changes: 3 additions & 2 deletions core/engine/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ use crate::{
use boa_macros::utf16;

use boa_profiler::Profiler;
use cow_utils::CowUtils;
use icu_normalizer::{ComposingNormalizer, DecomposingNormalizer};
use std::cmp::{max, min};

@@ -1727,9 +1728,9 @@ impl String {
// the Unicode Default Case Conversion algorithm.
let text = string.map_valid_segments(|s| {
if UPPER {
s.to_uppercase()
s.cow_to_uppercase().to_string()
} else {
s.to_lowercase()
s.cow_to_lowercase().to_string()
}
});

3 changes: 2 additions & 1 deletion core/engine/src/builtins/temporal/zoneddatetime/mod.rs
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ use crate::{
};
use boa_gc::{Finalize, Trace};
use boa_profiler::Profiler;
use cow_utils::CowUtils;
use num_traits::ToPrimitive;
use temporal_rs::{
options::{ArithmeticOverflow, Disambiguation, OffsetDisambiguation},
@@ -453,7 +454,7 @@ impl ZonedDateTime {

let era = zdt.inner.era_with_provider(context.tz_provider())?;
Ok(era
.map(|tinystr| JsString::from(tinystr.to_lowercase()))
.map(|tinystr| JsString::from(tinystr.cow_to_lowercase().to_string()))
.into_or_undefined())
}

1 change: 1 addition & 0 deletions core/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ rust-version.workspace = true
proc-macro = true

[dependencies]
cow-utils.workspace = true
quote.workspace = true
syn = { workspace = true, features = ["full"] }
proc-macro2.workspace = true
11 changes: 6 additions & 5 deletions core/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
)]
#![cfg_attr(not(test), forbid(clippy::unwrap_used))]

use cow_utils::CowUtils;
use proc_macro::TokenStream;
use proc_macro2::Literal;
use quote::{quote, ToTokens};
@@ -65,14 +66,14 @@ impl Parse for Static {
let ident = if let Some(ident) = ident {
syn::parse2::<Ident>(ident.into_token_stream())?
} else {
Ident::new(&literal.value().to_uppercase(), literal.span())
Ident::new(&literal.value().cow_to_uppercase(), literal.span())
};

Ok(Self { literal, ident })
}
Expr::Lit(expr) => match expr.lit {
Lit::Str(str) => Ok(Self {
ident: Ident::new(&str.value().to_uppercase(), str.span()),
ident: Ident::new(&str.value().cow_to_uppercase(), str.span()),
literal: str,
}),
_ => Err(syn::Error::new_spanned(
@@ -108,9 +109,9 @@ pub fn static_syms(input: TokenStream) -> TokenStream {
"Symbol for the \"{}\" string.",
lit.literal
.value()
.replace('<', r"\<")
.replace('>', r"\>")
.replace('*', r"\*")
.cow_replace('<', r"\<")
.cow_replace('>', r"\>")
.cow_replace('*', r"\*")
);
let ident = &lit.ident;
idx += 1;
1 change: 1 addition & 0 deletions tests/tester/Cargo.toml
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ phf = { workspace = true, features = ["macros"] }
comfy-table.workspace = true
serde_repr.workspace = true
bus.workspace = true
cow-utils.workspace = true

[features]
default = ["boa_engine/intl_bundled", "boa_engine/experimental", "boa_engine/annex-b"]
3 changes: 2 additions & 1 deletion tests/tester/src/read.rs
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ use color_eyre::{
eyre::{OptionExt, WrapErr},
Result,
};
use cow_utils::CowUtils;
use rustc_hash::{FxBuildHasher, FxHashMap};
use serde::Deserialize;

@@ -247,7 +248,7 @@ fn read_metadata(test: &Path) -> Result<MetaData> {
let (metadata, _) = metadata
.split_once("---*/")
.ok_or_eyre("invalid test metadata")?;
let metadata = metadata.replace('\r', "\n");
let metadata = metadata.cow_replace('\r', "\n");

serde_yaml::from_str(&metadata).map_err(Into::into)
}