5#ifndef V8_OBJECTS_STRING_H_
6#define V8_OBJECTS_STRING_H_
24#include "third_party/simdutf/simdutf.h"
32class CheckedInternalizedString;
33class BuiltinStringFromCharCode;
34class MaglevGraphBuilder;
84 inline uint32_t
type()
const {
return type_; }
86 inline bool valid()
const {
return valid_; }
92 return that.type_ == this->
type_;
98 inline void set_valid() { valid_ =
true; }
167#ifdef ENABLE_SLOW_DCHECKS
195#ifdef ENABLE_SLOW_DCHECKS
196 inline uint32_t ComputeChecksum()
const;
205 template <
typename IsolateT>
209 template <
typename Char>
216 template <
typename Char>
221 template <
typename Char>
231 inline uint32_t
length()
const;
251 inline void Set(uint32_t index, uint16_t value);
267 template <
template <
typename>
typename HandleType>
270 HandleType<String> subject);
289 template <
typename T,
template <
typename>
typename HandleType>
292 Isolate* isolate, HandleType<T>
string,
294 template <
typename T,
template <
typename>
typename HandleType>
320 template <
typename T,
template <
typename>
typename HandleType>
322 static inline HandleType<String>
Share(
Isolate* isolate,
323 HandleType<T>
string);
370 bool* capture_exists) = 0;
385 uint32_t start_index = 0);
399 template <EqualityType kEqType = EqualityType::kWholeString,
typename Char>
407 template <EqualityType kEqType = EqualityType::kWholeString,
typename Char>
415 template <EqualityType kEqType = EqualityType::kWholeString,
typename Char>
433 size_t* length_output =
nullptr);
436 size_t* length_output =
nullptr);
439 template <
typename T>
441 template <
typename T>
490#if defined(DEBUG) || defined(OBJECT_PRINT)
491 char* ToAsciiArray();
496 inline bool IsFlat()
const;
521 template <
typename SinkCharT>
524 uint32_t
start, uint32_t length);
525 template <
typename SinkCharT>
527 uint32_t
start, uint32_t length,
536 template <
typename SinkCharT>
539 uint32_t src_index, uint32_t length,
567 char* buffer,
size_t capacity,
569 size_t* processed_characters_return =
nullptr);
575 static inline bool IsAscii(
const char* chars, uint32_t length) {
576 return simdutf::validate_ascii(chars, length);
579 static inline bool IsAscii(
const uint8_t* chars, uint32_t length) {
580 return simdutf::validate_ascii(
reinterpret_cast<const char*
>(chars),
587 const uint16_t*
start = chars;
588 const uint16_t* limit = chars +
length;
594 return static_cast<uint32_t
>(chars -
start);
601#ifdef V8_TARGET_LITTLE_ENDIAN
606 while (chars +
sizeof(uintptr_t) <= limit) {
607 if (*
reinterpret_cast<const uintptr_t*
>(chars) & non_one_byte_mask) {
610 chars += (
sizeof(uintptr_t) /
sizeof(
base::uc16));
615 while (chars < limit) {
617 return static_cast<uint32_t
>(chars -
start);
622 return static_cast<uint32_t
>(chars -
start);
631 template <
class Visitor>
636 template <
class Visitor>
644 template <
typename IsolateT>
647 bool include_ending_line);
649 template <
typename IsolateT>
652 bool include_ending_line);
665 template <
typename TDispatcher>
669 -> std::common_type_t<
681 template <
typename TDispatcher,
typename... TArgs>
708 template <EqualityType kEqType,
typename Char>
714 template <
typename Char>
721 template <
template <
typename>
typename HandleType>
735 template <
template <
typename>
typename HandleType>
766 static constexpr int kHeaderSize =
sizeof(
String);
771 "String length must fit into a Smi");
775 "String::kMaxLength * 2 must fit into an int32");
780 "String object size in bytes must fit into a Smi");
784 "String object size in bytes must fit into an int");
832 return data_size == other.data_size && padding_size == other.padding_size;
835 DataAndPaddingSizes GetDataAndPaddingSizes()
const;
851 static const bool kHasOneByteEncoding =
true;
854 V8_INLINE static constexpr int32_t DataSizeFor(int32_t length);
855 V8_INLINE static constexpr int32_t SizeFor(int32_t length);
860 inline uint8_t
Get(uint32_t index)
const;
861 inline uint8_t
Get(uint32_t index,
863 inline void SeqOneByteStringSet(uint32_t index, uint16_t value);
864 inline void SeqOneByteStringSetChars(uint32_t index,
const uint8_t*
string,
868 inline Address GetCharsAddress()
const;
883 inline void clear_padding_destructively(uint32_t length);
886 static const uint32_t kMaxCharsSize = kMaxLength;
888 inline int AllocatedSize()
const;
918 static_assert(
static_cast<int>((
kMaxSize - kHeaderSize) /
990 static_assert(
static_cast<int>((
kMaxSize - kHeaderSize) /
1121 inline int32_t
offset()
const;
1242 inline const uint8_t*
GetChars()
const;
1245 inline uint8_t
Get(uint32_t index,
1280 inline const uint16_t*
GetChars()
const;
1283 inline uint16_t
Get(
1304 void PostGarbageCollection()
override;
1306 template <
typename Char>
1307 inline Char
Get(uint32_t index)
const;
1332 if (cons_string.
is_null())
return;
1351 "kStackSize must be power of two");
1375template <
typename Char>
static const uint16_t kMaxChar
static constexpr int kMaxLength
V8_EXPORT_PRIVATE Tagged< String > Continue(int *offset_out)
void Reset(Tagged< ConsString > cons_string, int offset=0)
void PushRight(Tagged< ConsString > string)
Tagged< String > NextLeaf(bool *blew_stack)
Tagged< ConsString > frames_[kStackSize]
Tagged< String > Search(int *offset_out)
V8_EXPORT_PRIVATE void Initialize(Tagged< ConsString > cons_string, int offset)
static int OffsetForDepth(int depth)
void AdjustMaximumDepth()
Tagged< String > Next(int *offset_out)
static const int kStackSize
ConsStringIterator & operator=(const ConsStringIterator &)=delete
Tagged< ConsString > root_
ConsStringIterator()=default
void PushLeft(Tagged< ConsString > string)
static const int kDepthMask
ConsStringIterator(const ConsStringIterator &)=delete
ConsStringIterator(Tagged< ConsString > cons_string, int offset=0)
V8_EXPORT_PRIVATE uint16_t Get(uint32_t index, const SharedStringAccessGuardIfNeeded &access_guard) const
static const uint32_t kMinLength
friend struct OffsetsForDebug
V8_INLINE bool IsFlat() const
void set_first(Tagged< String > value, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
Tagged< String > first() const
Tagged< Object > unchecked_second() const
friend class SandboxTesting
TaggedMember< String > second_
void set_second(Tagged< String > value, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
Tagged< Object > unchecked_first() const
TaggedMember< String > first_
Tagged< String > second() const
friend class TorqueGeneratedConsStringAsserts
Resource * mutable_resource()
static const bool kHasOneByteEncoding
const Resource * resource() const
const uint8_t * GetChars() const
void update_data_cache(Isolate *isolate)
void SetResource(Isolate *isolate, const Resource *buffer)
uint8_t Get(uint32_t index, const SharedStringAccessGuardIfNeeded &access_guard) const
void set_resource(Isolate *isolate, const Resource *buffer)
void VisitExternalPointers(ObjectVisitor *visitor)
friend struct OffsetsForDebug
void SetResourceRefForSerialization(uint32_t ref)
void DisposeResource(Isolate *isolate)
int ExternalPayloadSize() const
void InitExternalPointerFields(Isolate *isolate)
uint32_t GetResourceRefForDeserialization()
Address resource_as_address() const
friend class TorqueGeneratedExternalStringAsserts
void InitExternalPointerFieldsDuringExternalization(Tagged< Map > new_map, Isolate *isolate)
void set_address_as_resource(Isolate *isolate, Address address)
ExternalPointerMember< kExternalStringResourceDataTag > resource_data_
static const bool kHasOneByteEncoding
void SetResource(Isolate *isolate, const Resource *buffer)
uint16_t Get(uint32_t index, const SharedStringAccessGuardIfNeeded &access_guard) const
Resource * mutable_resource()
const uint16_t * GetChars() const
const uint16_t * ExternalTwoByteStringGetData(uint32_t start)
void set_resource(Isolate *isolate, const Resource *buffer)
void update_data_cache(Isolate *isolate)
const Resource * resource() const
DirectHandle< String > str_
static const int kStringResourceOffset
friend class StringBuiltinsAssembler
friend class ToDirectStringAssembler
static const uint32_t kMaxCharsSize
friend struct OffsetsForDebug
friend class SandboxTesting
friend class StringFromCharCodeAssembler
FLEXIBLE_ARRAY_MEMBER(Char, chars)
friend class CodeStubAssembler
friend class TorqueGeneratedSeqOneByteStringAsserts
friend class IntlBuiltinsAssembler
uint16_t Get(uint32_t index, const SharedStringAccessGuardIfNeeded &access_guard) const
static bool IsCompatibleMap(Tagged< Map > map, ReadOnlyRoots roots)
Address GetCharsAddress() const
friend struct OffsetsForDebug
int AllocatedSize() const
void clear_padding_destructively(uint32_t length)
static V8_INLINE constexpr int32_t DataSizeFor(int32_t length)
static const uint32_t kMaxCharsSize
friend class TorqueGeneratedSeqTwoByteStringAsserts
DataAndPaddingSizes GetDataAndPaddingSizes() const
friend class StringFromCharCodeAssembler
void SeqTwoByteStringSet(uint32_t index, uint16_t value)
static const bool kHasOneByteEncoding
static V8_INLINE constexpr int32_t SizeFor(int32_t length)
FLEXIBLE_ARRAY_MEMBER(Char, chars)
base::uc16 * GetChars(const DisallowGarbageCollection &no_gc)
friend struct OffsetsForDebug
Tagged< String > parent() const
V8_EXPORT_PRIVATE uint16_t Get(uint32_t index, const SharedStringAccessGuardIfNeeded &access_guard) const
friend class SandboxTesting
TaggedMember< Smi > offset_
void set_parent(Tagged< String > parent, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
static const uint32_t kMinLength
TaggedMember< String > parent_
friend class TorqueGeneratedSlicedStringAsserts
void set_offset(int32_t offset)
V8_INLINE bool IsShared() const
V8_INLINE uint32_t encoding_tag() const
V8_INLINE bool IsThin() const
V8_INLINE bool IsExternalOneByte() const
V8_INLINE bool IsInternalized() const
V8_INLINE StringShape(const Tagged< String > s)
V8_INLINE bool IsIndirect() const
V8_INLINE uint32_t representation_encoding_and_shared_tag() const
V8_INLINE bool IsCons() const
V8_INLINE StringRepresentationTag representation_tag() const
V8_INLINE bool IsSliced() const
V8_INLINE bool IsExternalTwoByte() const
bool operator==(const StringShape &that) const
V8_INLINE bool IsExternal() const
V8_INLINE bool IsSequentialOneByte() const
V8_INLINE uint32_t representation_and_encoding_tag() const
V8_INLINE bool IsDirect() const
V8_INLINE bool IsSequentialTwoByte() const
V8_INLINE bool IsUncachedExternal() const
V8_INLINE bool IsSequential() const
base::Vector< const uint8_t > ToOneByteVector() const
static constexpr uint32_t kChecksumVerificationDisabled
void UnsafeDisableChecksumVerification()
const uint8_t * onebyte_start
FlatContent(const DisallowGarbageCollection &no_gc)
bool UsesSameString(const FlatContent &other) const
base::Vector< const base::uc16 > ToUC16Vector() const
const DisallowGarbageCollection & no_gc_
FlatContent(const uint8_t *start, uint32_t length, const DisallowGarbageCollection &no_gc)
friend class IterableSubString
const base::uc16 * twobyte_start
base::uc16 Get(uint32_t i) const
virtual int CaptureCount()=0
virtual DirectHandle< String > GetMatch()=0
virtual MaybeDirectHandle< String > GetNamedCapture(DirectHandle< String > name, CaptureState *state)=0
virtual DirectHandle< String > GetSuffix()=0
virtual bool HasNamedCaptures()=0
virtual MaybeDirectHandle< String > GetCapture(int i, bool *capture_exists)=0
virtual DirectHandle< String > GetPrefix()=0
void StringShortPrint(StringStream *accumulator)
static V8_NOINLINE bool IsConsStringEqualToImpl(Tagged< ConsString > string, base::Vector< const Char > str, const SharedStringAccessGuardIfNeeded &access_guard)
static void WriteToFlat(Tagged< String > source, SinkCharT *sink, uint32_t start, uint32_t length)
V8_EXPORT_PRIVATE void PrintOn(FILE *out)
static const uint32_t kMaxHashCalcLength
void set_length(uint32_t hash)
friend class SharedStringTableInsertionKey
friend struct OffsetsForDebug
static const uint32_t kMaxLength
static V8_INLINE HandleType< String > Flatten(Isolate *isolate, HandleType< T > string, AllocationType allocation=AllocationType::kYoung)
V8_EXPORT_PRIVATE uint32_t ComputeAndSetRawHash()
static size_t WriteUtf8(Isolate *isolate, DirectHandle< String > string, char *buffer, size_t capacity, Utf8EncodingFlags flags, size_t *processed_characters_return=nullptr)
static int32_t ToArrayIndex(Address addr)
static Tagged< Object > IndexOf(Isolate *isolate, DirectHandle< Object > receiver, DirectHandle< Object > search, DirectHandle< Object > position)
const uint8_t * AddressOfCharacterAt(uint32_t start_index, const DisallowGarbageCollection &no_gc)
static LineEndsVector CalculateLineEndsVector(IsolateT *isolate, DirectHandle< String > string, bool include_ending_line)
V8_INLINE auto DispatchToSpecificType(TDispatcher &&dispatcher) const -> std::common_type_t< decltype(dispatcher(Tagged< SeqOneByteString >{})), decltype(dispatcher(Tagged< SeqTwoByteString >{})), decltype(dispatcher(Tagged< ExternalOneByteString >{})), decltype(dispatcher(Tagged< ExternalTwoByteString >{})), decltype(dispatcher(Tagged< ThinString >{})), decltype(dispatcher(Tagged< ConsString >{})), decltype(dispatcher(Tagged< SlicedString >{}))>
static uint32_t constexpr kInlineLineEndsSize
V8_INLINE uint16_t GetImpl(uint32_t index, const SharedStringAccessGuardIfNeeded &access_guard) const
static bool IsAscii(const char *chars, uint32_t length)
static const uint32_t kMaxOneByteCharCodeU
bool IsTwoByteRepresentation() const
static bool IsAscii(const uint8_t *chars, uint32_t length)
V8_EXPORT_PRIVATE static V8_INLINE std::optional< FlatContent > TryGetFlatContentFromDirectString(const DisallowGarbageCollection &no_gc, Tagged< String > string, uint32_t offset, uint32_t length, const SharedStringAccessGuardIfNeeded &)
static auto DispatchToSpecificTypeWithoutCast(InstanceType instance_type, TArgs &&... args)
static bool IsIdentifier(Isolate *isolate, DirectHandle< String > str)
static const int32_t kMaxOneByteCharCode
static V8_WARN_UNUSED_RESULT ComparisonResult Compare(Isolate *isolate, DirectHandle< String > x, DirectHandle< String > y)
void MakeExternalDuringGC(Isolate *isolate, T *resource)
static uint32_t NonOneByteStart(const base::uc16 *chars, uint32_t length)
void MakeThin(IsolateT *isolate, Tagged< String > canonical)
V8_INLINE base::Vector< const Char > GetCharVector(const DisallowGarbageCollection &no_gc)
const char * PrefixForDebugPrint() const
V8_EXPORT_PRIVATE bool SlowAsArrayIndex(uint32_t *index)
static bool IsInPlaceInternalizable(Tagged< String > string)
static HandleType< String > Share(Isolate *isolate, HandleType< T > string)
V8_EXPORT_PRIVATE bool HasOneBytePrefix(base::Vector< const char > str)
static bool IsOneByteRepresentationUnderneath(Tagged< String > string)
V8_EXPORT_PRIVATE bool IsOneByteEqualTo(base::Vector< const char > str)
V8_EXPORT_PRIVATE FlatContent SlowGetFlatContent(const DisallowGarbageCollection &no_gc, const SharedStringAccessGuardIfNeeded &)
V8_EXPORT_PRIVATE bool SlowAsIntegerIndex(size_t *index)
static V8_WARN_UNUSED_RESULT MaybeDirectHandle< String > GetSubstitution(Isolate *isolate, Match *match, DirectHandle< String > replacement, uint32_t start_index=0)
friend class SandboxTesting
static const uint32_t kMaxUtf16CodeUnitU
static const base::uc32 kMaxCodePoint
static const int kMaxUtf16CodeUnit
static size_t Utf8Length(Isolate *isolate, DirectHandle< String > string)
V8_EXPORT_PRIVATE bool SlowEquals(Tagged< String > other) const
V8_INLINE uint16_t Get(uint32_t index) const
bool IsOneByteRepresentation() const
static Handle< FixedArray > CalculateLineEnds(IsolateT *isolate, DirectHandle< String > string, bool include_ending_line)
static V8_INLINE HandleType< String > Flatten(LocalIsolate *isolate, HandleType< T > string, AllocationType allocation=AllocationType::kYoung)
void Set(uint32_t index, uint16_t value)
static V8_EXPORT_PRIVATE HandleType< String > SlowFlatten(Isolate *isolate, HandleType< ConsString > cons, AllocationType allocation)
static HandleType< Number > ToNumber(Isolate *isolate, HandleType< String > subject)
V8_INLINE bool IsEqualToImpl(base::Vector< const Char > str, const SharedStringAccessGuardIfNeeded &access_guard) const
EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE) static HandleType< String > SlowShare(Isolate *isolate
const char * SuffixForDebugPrint() const
bool IsEqualTo(base::Vector< const Char > str, Isolate *isolate) const
bool AsArrayIndex(uint32_t *index)
static bool IsWellFormedUnicode(Isolate *isolate, DirectHandle< String > string)
static void WriteToFlat2(SinkCharT *dst, Tagged< ConsString > src, uint32_t src_index, uint32_t length, const SharedStringAccessGuardIfNeeded &aguard, const DisallowGarbageCollection &no_gc)
Tagged< String > GetUnderlying() const
static bool IsOneByte(const base::uc16 *chars, uint32_t length)
V8_EXPORT_PRIVATE bool MakeExternal(Isolate *isolate, v8::String::ExternalStringResource *resource)
friend class wasm::baseline::LiftoffCompiler
friend class TorqueGeneratedStringAsserts
bool Equals(Tagged< String > other) const
std::unique_ptr< char[]> ToCString(uint32_t offset, uint32_t length, size_t *length_output=nullptr)
static const uint32_t kMaxShortPrintLength
const Char * GetDirectStringChars(const DisallowGarbageCollection &no_gc) const
static bool IsInPlaceInternalizableExcludingExternal(InstanceType instance_type)
void PrintUC16(std::ostream &os, int start=0, int end=-1)
V8_EXPORT_PRIVATE V8_INLINE FlatContent GetFlatContent(const DisallowGarbageCollection &no_gc)
bool SupportsExternalization(v8::String::Encoding)
V8_OBJECT_INNER_CLASS class v8::internal::String::FlatContent V8_OBJECT_INNER_CLASS_END
static Tagged< Object > LastIndexOf(Isolate *isolate, DirectHandle< Object > receiver, DirectHandle< Object > search, DirectHandle< Object > position)
static Tagged< ConsString > VisitFlat(Visitor *visitor, Tagged< String > string, int offset=0)
bool AsIntegerIndex(size_t *index)
bool MarkForExternalizationDuringGC(Isolate *isolate, T *resource)
const DisallowGarbageCollection & no_gc_
V8_INLINE constexpr bool is_null() const
Tagged< String > actual() const
friend struct OffsetsForDebug
TaggedMember< String > actual_
V8_EXPORT_PRIVATE uint16_t Get(uint32_t index, const SharedStringAccessGuardIfNeeded &access_guard) const
friend class TorqueGeneratedThinStringAsserts
Tagged< HeapObject > unchecked_actual() const
void set_actual(Tagged< String > value, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
ExternalPointerMember< kExternalStringResourceTag > resource_
#define OBJECT_POINTER_ALIGN(value)
base::Vector< const DirectHandle< Object > > args
#define EXPORT_TEMPLATE_DECLARE(export)
constexpr bool IsPowerOfTwo(T value)
V8_INLINE const Operation & Get(const Graph &graph, OpIndex index)
Tagged(T object) -> Tagged< T >
constexpr int kUIntptrSize
v8::internal::LoadHandler V8_OBJECT_END
constexpr uintptr_t kUintptrAllBitsSet
#define V8_OBJECT_INNER_CLASS
#define DECL_VERIFIER(Name)
#define DECL_PRINTER(Name)
#define EXPORT_DECL_VERIFIER(Name)
BytecodeSequenceNode * parent_
static constexpr int kMaxSize
#define DCHECK(condition)
#define DCHECK_EQ(v1, v2)
#define V8_EXPORT_PRIVATE
constexpr bool IsAligned(T value, U alignment)
bool operator==(const DataAndPaddingSizes &other) const
#define V8_WARN_UNUSED_RESULT