1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::borrow::Borrow;
use std::fmt::Debug;
use std::hash::Hash;
use std::sync::Arc;
use url::Url;
use v8::NewStringType;
/// Module names and code can be sourced from strings or bytes that are either owned or borrowed. This enumeration allows us
/// to perform a minimal amount of cloning and format-shifting of the underlying data.
///
/// Note that any [`FastString`] created from a `'static` byte array or string must contain ASCII characters.
///
/// Examples of ways to construct a [`FastString`]:
///
/// ```rust
/// # use deno_core::{ascii_str, FastString};
///
/// let code: FastString = ascii_str!("a string");
/// let code: FastString = format!("a string").into();
/// ```
pub enum FastString {
/// Created from static data.
Static(&'static str),
/// Created from static data, known to contain only ASCII chars.
StaticAscii(&'static str),
/// An owned chunk of data. Note that we use `Box` rather than `Vec` to avoid the
/// storage overhead.
Owned(Box<str>),
// Scripts loaded from the `deno_graph` infrastructure.
Arc(Arc<str>),
}
impl FastString {
/// Compile-time function to determine if a string is ASCII. Note that UTF-8 chars
/// longer than one byte have the high-bit set and thus, are not ASCII.
const fn is_ascii(s: &'static [u8]) -> bool {
let mut i = 0;
while i < s.len() {
if !s[i].is_ascii() {
return false;
}
i += 1;
}
true
}
/// Create a [`FastString`] from a static string. The string may contain non-ASCII characters, and if
/// so, will take the slower path when used in v8.
pub const fn from_static(s: &'static str) -> Self {
if Self::is_ascii(s.as_bytes()) {
Self::StaticAscii(s)
} else {
Self::Static(s)
}
}
/// Create a [`FastString`] from a static string. If the string contains non-ASCII characters, the compiler
/// will abort.
pub const fn ensure_static_ascii(s: &'static str) -> Self {
if Self::is_ascii(s.as_bytes()) {
Self::StaticAscii(s)
} else {
panic!("This string contained non-ASCII characters and cannot be created with ensure_static_ascii")
}
}
/// Creates a cheap copy of this [`FastString`], potentially transmuting it to a faster form. Note that this
/// is not a clone operation as it consumes the old [`FastString`].
pub fn into_cheap_copy(self) -> (Self, Self) {
match self {
Self::Static(s) => (Self::Static(s), Self::Static(s)),
Self::StaticAscii(s) => (Self::StaticAscii(s), Self::StaticAscii(s)),
Self::Arc(s) => (Self::Arc(s.clone()), Self::Arc(s)),
Self::Owned(s) => {
let s: Arc<str> = s.into();
(Self::Arc(s.clone()), Self::Arc(s))
}
}
}
/// If this [`FastString`] is cheaply cloneable, returns a clone.
pub fn try_clone(&self) -> Option<Self> {
match self {
Self::Static(s) => Some(Self::Static(s)),
Self::StaticAscii(s) => Some(Self::StaticAscii(s)),
Self::Arc(s) => Some(Self::Arc(s.clone())),
Self::Owned(_s) => None,
}
}
pub const fn try_static_ascii(&self) -> Option<&'static [u8]> {
match self {
Self::StaticAscii(s) => Some(s.as_bytes()),
_ => None,
}
}
pub fn as_bytes(&self) -> &[u8] {
// TODO(mmastrac): This can be const eventually (waiting for Arc const deref)
match self {
Self::Arc(s) => s.as_bytes(),
Self::Owned(s) => s.as_bytes(),
Self::Static(s) => s.as_bytes(),
Self::StaticAscii(s) => s.as_bytes(),
}
}
pub fn as_str(&self) -> &str {
// TODO(mmastrac): This can be const eventually (waiting for Arc const deref)
match self {
Self::Arc(s) => s,
Self::Owned(s) => s,
Self::Static(s) => s,
Self::StaticAscii(s) => s,
}
}
/// Create a v8 string from this [`FastString`]. If the string is static and contains only ASCII characters,
/// an external one-byte static is created.
pub fn v8<'a>(
&self,
scope: &mut v8::HandleScope<'a>,
) -> v8::Local<'a, v8::String> {
match self.try_static_ascii() {
Some(s) => v8::String::new_external_onebyte_static(scope, s).unwrap(),
None => {
v8::String::new_from_utf8(scope, self.as_bytes(), NewStringType::Normal)
.unwrap()
}
}
}
/// Truncates a [`FastString`] value, possibly re-allocating or memcpy'ing. May be slow.
pub fn truncate(&mut self, index: usize) {
match self {
Self::Static(b) => *self = Self::Static(&b[..index]),
Self::StaticAscii(b) => *self = Self::StaticAscii(&b[..index]),
Self::Owned(b) => *self = Self::Owned(b[..index].to_owned().into()),
// We can't do much if we have an Arc<str>, so we'll just take ownership of the truncated version
Self::Arc(s) => *self = s[..index].to_owned().into(),
}
}
}
impl Hash for FastString {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state)
}
}
impl AsRef<str> for FastString {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for FastString {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl Debug for FastString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self.as_str(), f)
}
}
impl Default for FastString {
fn default() -> Self {
Self::StaticAscii("")
}
}
impl PartialEq for FastString {
fn eq(&self, other: &Self) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl Eq for FastString {}
/// [`FastString`] can be made cheaply from [`Url`] as we know it's owned and don't need to do an
/// ASCII check.
impl From<Url> for FastString {
fn from(value: Url) -> Self {
let s: String = value.into();
s.into()
}
}
/// [`FastString`] can be made cheaply from [`String`] as we know it's owned and don't need to do an
/// ASCII check.
impl From<String> for FastString {
fn from(value: String) -> Self {
FastString::Owned(value.into_boxed_str())
}
}
/// [`FastString`] can be made cheaply from [`Arc<str>`] as we know it's shared and don't need to do an
/// ASCII check.
impl From<Arc<str>> for FastString {
fn from(value: Arc<str>) -> Self {
FastString::Arc(value)
}
}
/// Include a fast string in the binary. This string is asserted at compile-time to be 7-bit ASCII for optimal
/// v8 performance.
#[macro_export]
macro_rules! include_ascii_string {
($file:literal) => {
$crate::FastString::ensure_static_ascii(include_str!($file))
};
}
/// Include a fast string in the binary from a string literal. This string is asserted at compile-time to be
/// 7-bit ASCII for optimal v8 performance.
#[macro_export]
macro_rules! ascii_str {
($str:literal) => {
$crate::FastString::ensure_static_ascii($str)
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn truncate() {
let mut s = "123456".to_owned();
s.truncate(3);
let mut code: FastString = FastString::from_static("123456");
code.truncate(3);
assert_eq!(s, code.as_ref());
let mut code: FastString = "123456".to_owned().into();
code.truncate(3);
assert_eq!(s, code.as_ref());
let arc_str: Arc<str> = "123456".into();
let mut code: FastString = arc_str.into();
code.truncate(3);
assert_eq!(s, code.as_ref());
}
}