Skip to content

Commit

Permalink
core: Minor StringContext API changes
Browse files Browse the repository at this point in the history
- Rename `get_char` to `make_char`, and `get_ascii_char` to `ascii_char`:
  this makes clearer that only `make_char` may allocate.
- Take a `u8` instead of a `char` in `ascii_char`.
- Take a `Range<usize>` instead of two `usize` args in `substring`.
  • Loading branch information
moulins committed Oct 2, 2024
1 parent 03aad54 commit 9cc1406
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion core/src/avm2/globals/avmplus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ fn display_name<'gc>(
if let Some(name) = name {
name.to_qualified_name_or_star(context)
} else {
context.get_ascii_char('*')
context.ascii_char(b'*')
}
}

Expand Down
10 changes: 5 additions & 5 deletions core/src/avm2/globals/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn char_at<'gc>(

let index = if !n.is_nan() { n as usize } else { 0 };
let ret = if let Some(c) = s.get(index) {
activation.strings().get_char(c)
activation.strings().make_char(c)
} else {
activation.strings().empty()
};
Expand Down Expand Up @@ -449,7 +449,7 @@ fn slice<'gc>(
if start_index < end_index {
Ok(activation
.strings()
.substring(this, start_index, end_index)
.substring(this, start_index..end_index)
.into())
} else {
Ok("".into())
Expand Down Expand Up @@ -486,7 +486,7 @@ fn split<'gc>(
// Special case this to match Flash's behavior.
this.iter()
.take(limit)
.map(|c| Value::from(activation.strings().get_char(c)))
.map(|c| Value::from(activation.strings().make_char(c)))
.collect()
} else {
this.split(&delimiter)
Expand Down Expand Up @@ -551,7 +551,7 @@ fn substr<'gc>(

Ok(activation
.strings()
.substring(this, start_index, end_index)
.substring(this, start_index..end_index)
.into())
}

Expand Down Expand Up @@ -588,7 +588,7 @@ fn substring<'gc>(

Ok(activation
.strings()
.substring(this, start_index, end_index)
.substring(this, start_index..end_index)
.into())
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/multiname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl<'gc> Multiname<'gc> {
/// This is used by `describeType`
pub fn to_qualified_name_or_star(&self, context: &mut StringContext<'gc>) -> AvmString<'gc> {
if self.is_any_name() {
context.get_ascii_char('*')
context.ascii_char(b'*')
} else {
self.to_qualified_name(context.gc_context)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<'gc> PropertyClass<'gc> {
match self {
PropertyClass::Class(class) => class.name().to_qualified_name(context.gc_context),
PropertyClass::Name(name, _) => name.to_qualified_name_or_star(context),
PropertyClass::Any => context.get_ascii_char('*'),
PropertyClass::Any => context.ascii_char(b'*'),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ impl<'gc> Value<'gc> {
}
Value::Integer(i) => {
if *i >= 0 && *i < 10 {
activation.strings().get_char('0' as u16 + *i as u16)
activation.strings().make_char('0' as u16 + *i as u16)
} else {
AvmString::new_utf8(activation.context.gc_context, i.to_string())
}
Expand Down
17 changes: 6 additions & 11 deletions core/src/string/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::{borrow::Cow, ops::Range};

use gc_arena::{Gc, Mutation};

Expand Down Expand Up @@ -76,28 +76,23 @@ impl<'gc> StringContext<'gc> {
}

#[must_use]
pub fn get_char(&self, c: u16) -> AvmString<'gc> {
pub fn make_char(&self, c: u16) -> AvmString<'gc> {
if let Some(s) = self.interner.chars.get(c as usize) {
(*s).into()
} else {
AvmString::new(self.gc(), WString::from_unit(c))
}
}

// Like get_char, but panics if the passed char is not ASCII.
/// Like `make_char`, but panics if the passed char is not ASCII.
#[must_use]
pub fn get_ascii_char(&self, c: char) -> AvmString<'gc> {
pub fn ascii_char(&self, c: u8) -> AvmString<'gc> {
self.interner.chars[c as usize].into()
}

#[must_use]
pub fn substring(
&self,
s: AvmString<'gc>,
start_index: usize,
end_index: usize,
) -> AvmString<'gc> {
pub fn substring(&self, s: AvmString<'gc>, range: Range<usize>) -> AvmString<'gc> {
self.interner
.substring(self.gc(), s, start_index, end_index)
.substring(self.gc(), s, range.start, range.end)
}
}

0 comments on commit 9cc1406

Please sign in to comment.