Skip to content

Commit

Permalink
StarlarkStr::repr
Browse files Browse the repository at this point in the history
Summary:
Format `&str` as `repr(s)`.

Used later in D50105958.

Reviewed By: JakobDegen

Differential Revision: D50104777

fbshipit-source-id: 713fe0b7a6c233ee72425c3c276f3ecca9403e48
  • Loading branch information
stepancheg authored and facebook-github-bot committed Oct 10, 2023
1 parent 75afe29 commit dc47941
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions starlark/src/values/types/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ impl StarlarkStr {
pub(crate) fn offset_of_content() -> usize {
memoffset::offset_of!(StarlarkStrN<0>, body)
}

/// Format a Rust string like `repr(s)`.
pub fn repr(s: &str) -> String {
let mut buffer = String::new();
string_repr(s, &mut buffer);
buffer
}
}

/// How to hash a string in a way that is compatible with Value
Expand All @@ -208,9 +215,7 @@ impl Display for StarlarkStr {
// We could either accumulate straight into the buffer (can't preallocate, virtual call on each character)
// or accumulate into a String buffer first. Not sure which is faster, but string buffer lets us
// share code with collect_repr more easily.
let mut buffer = String::new();
string_repr(self.as_str(), &mut buffer);
f.write_str(&buffer)
f.write_str(&StarlarkStr::repr(self.as_str()))
}
}

Expand Down

0 comments on commit dc47941

Please sign in to comment.