#[repr(C)]pub struct String(/* private fields */);
Expand description
A JavaScript string value (ECMA-262, 4.3.17).
Implementations§
Source§impl String
impl String
Sourcepub const MAX_LENGTH: usize = 536_870_888usize
pub const MAX_LENGTH: usize = 536_870_888usize
The maximum length (in bytes) of a buffer that a v8::String can be built from. Attempting to create a v8::String from a larger buffer will result in None being returned.
pub fn empty<'s>(scope: &mut HandleScope<'s, ()>) -> Local<'s, String>
Sourcepub fn new_from_utf8<'s>(
scope: &mut HandleScope<'s, ()>,
buffer: &[u8],
new_type: NewStringType,
) -> Option<Local<'s, String>>
pub fn new_from_utf8<'s>( scope: &mut HandleScope<'s, ()>, buffer: &[u8], new_type: NewStringType, ) -> Option<Local<'s, String>>
Allocates a new string from UTF-8 data. Only returns an empty value when length > kMaxLength
Sourcepub fn new_from_one_byte<'s>(
scope: &mut HandleScope<'s, ()>,
buffer: &[u8],
new_type: NewStringType,
) -> Option<Local<'s, String>>
pub fn new_from_one_byte<'s>( scope: &mut HandleScope<'s, ()>, buffer: &[u8], new_type: NewStringType, ) -> Option<Local<'s, String>>
Allocates a new string from Latin-1 data. Only returns an empty value when length > kMaxLength.
Sourcepub fn new_from_two_byte<'s>(
scope: &mut HandleScope<'s, ()>,
buffer: &[u16],
new_type: NewStringType,
) -> Option<Local<'s, String>>
pub fn new_from_two_byte<'s>( scope: &mut HandleScope<'s, ()>, buffer: &[u16], new_type: NewStringType, ) -> Option<Local<'s, String>>
Allocates a new string from UTF-16 data. Only returns an empty value when length > kMaxLength.
Sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
Returns the number of characters (UTF-16 code units) in this string.
Sourcepub fn utf8_length(&self, scope: &mut Isolate) -> usize
pub fn utf8_length(&self, scope: &mut Isolate) -> usize
Returns the number of bytes in the UTF-8 encoded representation of this string.
Sourcepub fn write(
&self,
scope: &mut Isolate,
buffer: &mut [u16],
start: usize,
options: WriteOptions,
) -> usize
pub fn write( &self, scope: &mut Isolate, buffer: &mut [u16], start: usize, options: WriteOptions, ) -> usize
Writes the contents of the string to an external buffer, as 16-bit (UTF-16) character codes.
Sourcepub fn write_one_byte(
&self,
scope: &mut Isolate,
buffer: &mut [u8],
start: usize,
options: WriteOptions,
) -> usize
pub fn write_one_byte( &self, scope: &mut Isolate, buffer: &mut [u8], start: usize, options: WriteOptions, ) -> usize
Writes the contents of the string to an external buffer, as one-byte (Latin-1) characters.
Sourcepub fn write_one_byte_uninit(
&self,
scope: &mut Isolate,
buffer: &mut [MaybeUninit<u8>],
start: usize,
options: WriteOptions,
) -> usize
pub fn write_one_byte_uninit( &self, scope: &mut Isolate, buffer: &mut [MaybeUninit<u8>], start: usize, options: WriteOptions, ) -> usize
Writes the contents of the string to an external MaybeUninit
buffer, as one-byte
(Latin-1) characters.
Sourcepub fn write_utf8(
&self,
scope: &mut Isolate,
buffer: &mut [u8],
nchars_ref: Option<&mut usize>,
options: WriteOptions,
) -> usize
pub fn write_utf8( &self, scope: &mut Isolate, buffer: &mut [u8], nchars_ref: Option<&mut usize>, options: WriteOptions, ) -> usize
Writes the contents of the string to an external buffer, as UTF-8.
Sourcepub fn write_utf8_uninit(
&self,
scope: &mut Isolate,
buffer: &mut [MaybeUninit<u8>],
nchars_ref: Option<&mut usize>,
options: WriteOptions,
) -> usize
pub fn write_utf8_uninit( &self, scope: &mut Isolate, buffer: &mut [MaybeUninit<u8>], nchars_ref: Option<&mut usize>, options: WriteOptions, ) -> usize
Writes the contents of the string to an external MaybeUninit
buffer, as UTF-8.
pub fn new<'s>( scope: &mut HandleScope<'s, ()>, value: &str, ) -> Option<Local<'s, String>>
Sourcepub const fn create_external_onebyte_const(
buffer: &'static [u8],
) -> OneByteConst
pub const fn create_external_onebyte_const( buffer: &'static [u8], ) -> OneByteConst
Compile-time function to create an external string resource. The buffer is checked to contain only ASCII characters.
Sourcepub const unsafe fn create_external_onebyte_const_unchecked(
buffer: &'static [u8],
) -> OneByteConst
pub const unsafe fn create_external_onebyte_const_unchecked( buffer: &'static [u8], ) -> OneByteConst
Compile-time function to create an external string resource which skips the ASCII and length checks.
§Safety
The passed in buffer must contain only ASCII data. Note that while V8 allows OneByte string resources to contain Latin-1 data, the OneByteConst struct does not allow it.
Sourcepub fn new_from_onebyte_const<'s>(
scope: &mut HandleScope<'s, ()>,
onebyte_const: &'static OneByteConst,
) -> Option<Local<'s, String>>
pub fn new_from_onebyte_const<'s>( scope: &mut HandleScope<'s, ()>, onebyte_const: &'static OneByteConst, ) -> Option<Local<'s, String>>
Creates a v8::String from a &'static OneByteConst
which is guaranteed to be ASCII.
Note that OneByteConst guarantees ASCII even though V8 would allow OneByte string resources to contain Latin-1.
Sourcepub fn new_external_onebyte_static<'s>(
scope: &mut HandleScope<'s, ()>,
buffer: &'static [u8],
) -> Option<Local<'s, String>>
pub fn new_external_onebyte_static<'s>( scope: &mut HandleScope<'s, ()>, buffer: &'static [u8], ) -> Option<Local<'s, String>>
Creates a v8::String from a &'static [u8]
,
must be Latin-1 or ASCII, not UTF-8!
Sourcepub fn new_external_onebyte<'s>(
scope: &mut HandleScope<'s, ()>,
buffer: Box<[u8]>,
) -> Option<Local<'s, String>>
pub fn new_external_onebyte<'s>( scope: &mut HandleScope<'s, ()>, buffer: Box<[u8]>, ) -> Option<Local<'s, String>>
Creates a v8::String
from owned bytes.
The bytes must be Latin-1 or ASCII.
V8 will take ownership of the buffer and free it when the string is garbage collected.
Sourcepub unsafe fn new_external_onebyte_raw<'s>(
scope: &mut HandleScope<'s, ()>,
buffer: *mut char,
buffer_len: usize,
destructor: extern "C" fn(_: *mut char, _: usize),
) -> Option<Local<'s, String>>
pub unsafe fn new_external_onebyte_raw<'s>( scope: &mut HandleScope<'s, ()>, buffer: *mut char, buffer_len: usize, destructor: extern "C" fn(_: *mut char, _: usize), ) -> Option<Local<'s, String>>
Creates a v8::String
from owned bytes, length, and a custom destructor.
The bytes must be Latin-1 or ASCII.
V8 will take ownership of the buffer and free it when the string is garbage collected.
SAFETY: buffer
must be owned (valid for the lifetime of the string), and
destructor
must be a valid function pointer that can free the buffer.
The destructor will be called with the buffer and length when the string is garbage collected.
Sourcepub fn new_external_twobyte_static<'s>(
scope: &mut HandleScope<'s, ()>,
buffer: &'static [u16],
) -> Option<Local<'s, String>>
pub fn new_external_twobyte_static<'s>( scope: &mut HandleScope<'s, ()>, buffer: &'static [u16], ) -> Option<Local<'s, String>>
Creates a v8::String from a &'static [u16]
.
Sourcepub fn get_external_string_resource(
&self,
) -> Option<NonNull<ExternalStringResource>>
pub fn get_external_string_resource( &self, ) -> Option<NonNull<ExternalStringResource>>
Get the ExternalStringResource for an external string.
Returns None if is_external() doesn’t return true.
Sourcepub fn get_external_onebyte_string_resource(
&self,
) -> Option<NonNull<ExternalOneByteStringResource>>
pub fn get_external_onebyte_string_resource( &self, ) -> Option<NonNull<ExternalOneByteStringResource>>
Get the ExternalOneByteStringResource for an external one-byte string.
Returns None if is_external_onebyte() doesn’t return true.
Sourcepub fn get_external_string_resource_base(
&self,
) -> (Option<NonNull<ExternalStringResourceBase>>, Encoding)
pub fn get_external_string_resource_base( &self, ) -> (Option<NonNull<ExternalStringResourceBase>>, Encoding)
Get the ExternalStringResourceBase for an external string.
Note this is just the base class, and isn’t very useful on its own.
You’ll want to downcast to one of its subclasses, for instance
with get_external_onebyte_string_resource
.
Sourcepub fn is_external(&self) -> bool
pub fn is_external(&self) -> bool
True if string is external
Sourcepub fn is_external_onebyte(&self) -> bool
pub fn is_external_onebyte(&self) -> bool
True if string is external & one-byte (e.g: created with new_external_onebyte_static)
Sourcepub fn is_external_twobyte(&self) -> bool
pub fn is_external_twobyte(&self) -> bool
True if string is external & two-byte (e.g: created with new_external_twobyte_static)
Sourcepub fn is_onebyte(&self) -> bool
pub fn is_onebyte(&self) -> bool
Will return true if and only if string is known for certain to contain only one-byte data, ie: Latin-1, a.k.a. ISO-8859-1 code points. Doesn’t read the string so can return false negatives, and a return value of false does not mean this string is not one-byte data.
For a method that will not return false negatives at the cost of
potentially reading the entire string, use contains_only_onebyte()
.
Sourcepub fn contains_only_onebyte(&self) -> bool
pub fn contains_only_onebyte(&self) -> bool
True if the string contains only one-byte data. Will read the entire string in some cases.
Sourcepub fn to_rust_string_lossy(&self, scope: &mut Isolate) -> String
pub fn to_rust_string_lossy(&self, scope: &mut Isolate) -> String
Creates a copy of a crate::String
in a std::string::String
.
Convenience function not present in the original V8 API.
Sourcepub fn to_rust_cow_lossy<'a, const N: usize>(
&self,
scope: &mut Isolate,
buffer: &'a mut [MaybeUninit<u8>; N],
) -> Cow<'a, str>
pub fn to_rust_cow_lossy<'a, const N: usize>( &self, scope: &mut Isolate, buffer: &'a mut [MaybeUninit<u8>; N], ) -> Cow<'a, str>
Converts a crate::String
to either an owned std::string::String
, or a borrowed str
, depending on whether it fits into the
provided buffer.
Methods from Deref<Target = Name>§
Sourcepub fn get_identity_hash(&self) -> NonZeroI32
pub fn get_identity_hash(&self) -> NonZeroI32
Returns the V8 hash value for this value. The current implementation uses a hidden property to store the identity hash.
The return value will never be 0. Also, it is not guaranteed to be unique.
Methods from Deref<Target = Value>§
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Returns true if this value is the undefined value. See ECMA-262 4.3.10.
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true if this value is the null value. See ECMA-262 4.3.11.
Sourcepub fn is_null_or_undefined(&self) -> bool
pub fn is_null_or_undefined(&self) -> bool
Returns true if this value is either the null or the undefined value. See ECMA-262 4.3.11. and 4.3.12
Sourcepub fn is_true(&self) -> bool
pub fn is_true(&self) -> bool
Returns true if this value is true.
This is not the same as BooleanValue()
. The latter performs a
conversion to boolean, i.e. the result of Boolean(value)
in JS, whereas
this checks value === true
.
Sourcepub fn is_false(&self) -> bool
pub fn is_false(&self) -> bool
Returns true if this value is false.
This is not the same as !BooleanValue()
. The latter performs a
conversion to boolean, i.e. the result of !Boolean(value)
in JS, whereas
this checks value === false
.
Sourcepub fn is_name(&self) -> bool
pub fn is_name(&self) -> bool
Returns true if this value is a symbol or a string.
This is equivalent to
typeof value === 'string' || typeof value === 'symbol'
in JS.
Sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Returns true if this value is an instance of the String type. See ECMA-262 8.4.
Sourcepub fn is_symbol(&self) -> bool
pub fn is_symbol(&self) -> bool
Returns true if this value is a symbol.
This is equivalent to typeof value === 'symbol'
in JS.
Sourcepub fn is_function(&self) -> bool
pub fn is_function(&self) -> bool
Returns true if this value is a function.
Sourcepub fn is_array(&self) -> bool
pub fn is_array(&self) -> bool
Returns true if this value is an array. Note that it will return false for an Proxy for an array.
Sourcepub fn is_big_int(&self) -> bool
pub fn is_big_int(&self) -> bool
Returns true if this value is a bigint.
This is equivalent to typeof value === 'bigint'
in JS.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true if this value is boolean.
This is equivalent to typeof value === 'boolean'
in JS.
Sourcepub fn is_external(&self) -> bool
pub fn is_external(&self) -> bool
Returns true if this value is an External
object.
Sourcepub fn is_arguments_object(&self) -> bool
pub fn is_arguments_object(&self) -> bool
Returns true if this value is an Arguments object.
Sourcepub fn is_big_int_object(&self) -> bool
pub fn is_big_int_object(&self) -> bool
Returns true if this value is a BigInt object.
Sourcepub fn is_boolean_object(&self) -> bool
pub fn is_boolean_object(&self) -> bool
Returns true if this value is a Boolean object.
Sourcepub fn is_number_object(&self) -> bool
pub fn is_number_object(&self) -> bool
Returns true if this value is a Number object.
Sourcepub fn is_string_object(&self) -> bool
pub fn is_string_object(&self) -> bool
Returns true if this value is a String object.
Sourcepub fn is_symbol_object(&self) -> bool
pub fn is_symbol_object(&self) -> bool
Returns true if this value is a Symbol object.
Sourcepub fn is_native_error(&self) -> bool
pub fn is_native_error(&self) -> bool
Returns true if this value is a NativeError.
Sourcepub fn is_reg_exp(&self) -> bool
pub fn is_reg_exp(&self) -> bool
Returns true if this value is a RegExp.
Sourcepub fn is_async_function(&self) -> bool
pub fn is_async_function(&self) -> bool
Returns true if this value is an async function.
Sourcepub fn is_generator_function(&self) -> bool
pub fn is_generator_function(&self) -> bool
Returns true if this value is a Generator function.
Sourcepub fn is_promise(&self) -> bool
pub fn is_promise(&self) -> bool
Returns true if this value is a Promise.
Sourcepub fn is_map_iterator(&self) -> bool
pub fn is_map_iterator(&self) -> bool
Returns true if this value is a Map Iterator.
Sourcepub fn is_set_iterator(&self) -> bool
pub fn is_set_iterator(&self) -> bool
Returns true if this value is a Set Iterator.
Sourcepub fn is_generator_object(&self) -> bool
pub fn is_generator_object(&self) -> bool
Returns true if this value is a Generator Object.
Sourcepub fn is_weak_map(&self) -> bool
pub fn is_weak_map(&self) -> bool
Returns true if this value is a WeakMap.
Sourcepub fn is_weak_set(&self) -> bool
pub fn is_weak_set(&self) -> bool
Returns true if this value is a WeakSet.
Sourcepub fn is_array_buffer(&self) -> bool
pub fn is_array_buffer(&self) -> bool
Returns true if this value is an ArrayBuffer.
Sourcepub fn is_array_buffer_view(&self) -> bool
pub fn is_array_buffer_view(&self) -> bool
Returns true if this value is an ArrayBufferView.
Sourcepub fn is_typed_array(&self) -> bool
pub fn is_typed_array(&self) -> bool
Returns true if this value is one of TypedArrays.
Sourcepub fn is_uint8_array(&self) -> bool
pub fn is_uint8_array(&self) -> bool
Returns true if this value is an Uint8Array.
Sourcepub fn is_uint8_clamped_array(&self) -> bool
pub fn is_uint8_clamped_array(&self) -> bool
Returns true if this value is an Uint8ClampedArray.
Sourcepub fn is_int8_array(&self) -> bool
pub fn is_int8_array(&self) -> bool
Returns true if this value is an Int8Array.
Sourcepub fn is_uint16_array(&self) -> bool
pub fn is_uint16_array(&self) -> bool
Returns true if this value is an Uint16Array.
Sourcepub fn is_int16_array(&self) -> bool
pub fn is_int16_array(&self) -> bool
Returns true if this value is an Int16Array.
Sourcepub fn is_uint32_array(&self) -> bool
pub fn is_uint32_array(&self) -> bool
Returns true if this value is an Uint32Array.
Sourcepub fn is_int32_array(&self) -> bool
pub fn is_int32_array(&self) -> bool
Returns true if this value is an Int32Array.
Sourcepub fn is_float32_array(&self) -> bool
pub fn is_float32_array(&self) -> bool
Returns true if this value is a Float32Array.
Sourcepub fn is_float64_array(&self) -> bool
pub fn is_float64_array(&self) -> bool
Returns true if this value is a Float64Array.
Sourcepub fn is_big_int64_array(&self) -> bool
pub fn is_big_int64_array(&self) -> bool
Returns true if this value is a BigInt64Array.
Sourcepub fn is_big_uint64_array(&self) -> bool
pub fn is_big_uint64_array(&self) -> bool
Returns true if this value is a BigUint64Array.
Sourcepub fn is_data_view(&self) -> bool
pub fn is_data_view(&self) -> bool
Returns true if this value is a DataView.
Returns true if this value is a SharedArrayBuffer. This is an experimental feature.
Sourcepub fn is_wasm_memory_object(&self) -> bool
pub fn is_wasm_memory_object(&self) -> bool
Returns true if this value is a WasmMemoryObject.
Sourcepub fn is_wasm_module_object(&self) -> bool
pub fn is_wasm_module_object(&self) -> bool
Returns true if this value is a WasmModuleObject.
Sourcepub fn is_module_namespace_object(&self) -> bool
pub fn is_module_namespace_object(&self) -> bool
Returns true if the value is a Module Namespace Object.
pub fn strict_equals(&self, that: Local<'_, Value>) -> bool
pub fn same_value(&self, that: Local<'_, Value>) -> bool
Sourcepub fn same_value_zero(&self, that: Local<'_, Value>) -> bool
pub fn same_value_zero(&self, that: Local<'_, Value>) -> bool
Implements the the abstract operation SameValueZero
, which is defined in
ECMA-262 6th edition § 7.2.10
(http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero).
This operation is used to compare values for the purpose of insertion into
a Set
, or determining whether Map
keys are equivalent. Its semantics
are almost the same as strict_equals()
and same_value()
, with the
following important distinctions:
- It considers
NaN
equal toNaN
(unlikestrict_equals()
). - It considers
-0
equal to0
(unlikesame_value()
).
pub fn to_big_int<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, BigInt>>
pub fn to_number<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, Number>>
pub fn to_string<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, String>>
Sourcepub fn to_rust_string_lossy(&self, scope: &mut HandleScope<'_>) -> String
pub fn to_rust_string_lossy(&self, scope: &mut HandleScope<'_>) -> String
Convenience function not present in the original V8 API.
pub fn to_detail_string<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, String>>
pub fn to_object<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, Object>>
pub fn to_integer<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, Integer>>
pub fn to_uint32<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, Uint32>>
pub fn to_int32<'s>( &self, scope: &mut HandleScope<'s>, ) -> Option<Local<'s, Int32>>
Sourcepub fn to_boolean<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, Boolean>
pub fn to_boolean<'s>( &self, scope: &mut HandleScope<'s, ()>, ) -> Local<'s, Boolean>
Perform the equivalent of Boolean(value) in JS. This can never fail.
pub fn instance_of( &self, scope: &mut HandleScope<'_>, object: Local<'_, Object>, ) -> Option<bool>
pub fn number_value(&self, scope: &mut HandleScope<'_>) -> Option<f64>
pub fn integer_value(&self, scope: &mut HandleScope<'_>) -> Option<i64>
pub fn uint32_value(&self, scope: &mut HandleScope<'_>) -> Option<u32>
pub fn int32_value(&self, scope: &mut HandleScope<'_>) -> Option<i32>
pub fn boolean_value(&self, scope: &mut HandleScope<'_, ()>) -> bool
Sourcepub fn get_hash(&self) -> NonZeroI32
pub fn get_hash(&self) -> NonZeroI32
Returns the V8 hash value for this value. The current implementation uses a hidden property to store the identity hash on some object types.
The return value will never be 0. Also, it is not guaranteed to be unique.
pub fn type_of<'s>(&self, scope: &mut HandleScope<'s, ()>) -> Local<'s, String>
Methods from Deref<Target = Data>§
Sourcepub fn is_big_int(&self) -> bool
pub fn is_big_int(&self) -> bool
Returns true if this data is a BigInt
.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true if this data is a Boolean
.
Sourcepub fn is_context(&self) -> bool
pub fn is_context(&self) -> bool
Returns true if this data is a Context
.
Sourcepub fn is_fixed_array(&self) -> bool
pub fn is_fixed_array(&self) -> bool
Returns true if this data is a FixedArray
.
Sourcepub fn is_function_template(&self) -> bool
pub fn is_function_template(&self) -> bool
Returns true if this data is a FunctionTemplate
.
Sourcepub fn is_module_request(&self) -> bool
pub fn is_module_request(&self) -> bool
Returns true if this data is a ModuleRequest
.
Sourcepub fn is_object_template(&self) -> bool
pub fn is_object_template(&self) -> bool
Returns true if this data is a ObjectTemplate
.
Sourcepub fn is_primitive(&self) -> bool
pub fn is_primitive(&self) -> bool
Returns true if this data is a Primitive
.
Sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Returns true if this data is a Private
.