Skip to content

Commit

Permalink
Use cow-utils instead (#4133)
Browse files Browse the repository at this point in the history
* Use  instead

* chore: clippy.toml

Co-authored-by: José Julián Espina <[email protected]>

* Use cow-utils instead

---------

Co-authored-by: José Julián Espina <[email protected]>
  • Loading branch information
heygsc and jedel1043 authored Jan 18, 2025
1 parent 95aa4cd commit 718dfd9
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 14 deletions.
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
Expand Up @@ -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"]
Expand Down
5 changes: 3 additions & 2 deletions cli/src/debug/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -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),
Expand Down
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
Expand Up @@ -8,6 +8,7 @@ use crate::{
};

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

/// Builtin javascript 'isFinite(number)' function.
///
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion core/engine/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}"),
})
}
Expand Down
5 changes: 3 additions & 2 deletions core/engine/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()
}
});

Expand Down
3 changes: 2 additions & 1 deletion core/engine/src/builtins/temporal/zoneddatetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
};
use boa_gc::{Finalize, Trace};
use boa_profiler::Profiler;
use cow_utils::CowUtils;
use num_traits::ToPrimitive;
use temporal_rs::{
options::{
Expand Down Expand Up @@ -458,7 +459,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())
}

Expand Down
1 change: 1 addition & 0 deletions core/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions core/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 2 additions & 1 deletion tests/tester/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use color_eyre::{
eyre::{OptionExt, WrapErr},
Result,
};
use cow_utils::CowUtils;
use rustc_hash::{FxBuildHasher, FxHashMap};
use serde::Deserialize;

Expand Down Expand Up @@ -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)
}

0 comments on commit 718dfd9

Please sign in to comment.