V8 Project
v8::Utf8WriterVisitor Class Reference
+ Collaboration diagram for v8::Utf8WriterVisitor:

Public Member Functions

 Utf8WriterVisitor (char *buffer, int capacity, bool skip_capacity_check, bool replace_invalid_utf8)
 
template<typename Char >
void Visit (const Char *chars, const int length)
 
bool IsDone ()
 
void VisitOneByteString (const uint8_t *chars, int length)
 
void VisitTwoByteString (const uint16_t *chars, int length)
 
int CompleteWrite (bool write_null, int *utf16_chars_read_out)
 

Static Public Member Functions

static int WriteEndCharacter (uint16_t character, int last_character, int remaining, char *const buffer, bool replace_invalid_utf8)
 

Private Member Functions

 DISALLOW_IMPLICIT_CONSTRUCTORS (Utf8WriterVisitor)
 

Private Attributes

bool early_termination_
 
int last_character_
 
char * buffer_
 
char *const start_
 
int capacity_
 
bool const skip_capacity_check_
 
bool const replace_invalid_utf8_
 
int utf16_chars_read_
 

Detailed Description

Definition at line 4567 of file api.cc.

Constructor & Destructor Documentation

◆ Utf8WriterVisitor()

v8::Utf8WriterVisitor::Utf8WriterVisitor ( char *  buffer,
int  capacity,
bool  skip_capacity_check,
bool  replace_invalid_utf8 
)
inline

Definition at line 4569 of file api.cc.

4574  : early_termination_(false),
4576  buffer_(buffer),
4577  start_(buffer),
4578  capacity_(capacity),
4579  skip_capacity_check_(capacity == -1 || skip_capacity_check),
4580  replace_invalid_utf8_(replace_invalid_utf8),
4581  utf16_chars_read_(0) {
4582  }
static const int kNoPreviousCharacter
Definition: unicode.h:97
bool const skip_capacity_check_
Definition: api.cc:4745
bool const replace_invalid_utf8_
Definition: api.cc:4746
bool early_termination_
Definition: api.cc:4740
char *const start_
Definition: api.cc:4743

Member Function Documentation

◆ CompleteWrite()

int v8::Utf8WriterVisitor::CompleteWrite ( bool  write_null,
int utf16_chars_read_out 
)
inline

Definition at line 4725 of file api.cc.

4725  {
4726  // Write out number of utf16 characters written to the stream.
4727  if (utf16_chars_read_out != NULL) {
4728  *utf16_chars_read_out = utf16_chars_read_;
4729  }
4730  // Only null terminate if all of the string was written and there's space.
4731  if (write_null &&
4732  !early_termination_ &&
4733  (capacity_ == -1 || (buffer_ - start_) < capacity_)) {
4734  *buffer_++ = '\0';
4735  }
4736  return static_cast<int>(buffer_ - start_);
4737  }
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL

References NULL.

Referenced by v8::String::WriteUtf8().

+ Here is the caller graph for this function:

◆ DISALLOW_IMPLICIT_CONSTRUCTORS()

v8::Utf8WriterVisitor::DISALLOW_IMPLICIT_CONSTRUCTORS ( Utf8WriterVisitor  )
private

◆ IsDone()

bool v8::Utf8WriterVisitor::IsDone ( )
inline

Definition at line 4713 of file api.cc.

4713  {
4714  return early_termination_;
4715  }

Referenced by v8::RecursivelySerializeToUtf8().

+ Here is the caller graph for this function:

◆ Visit()

template<typename Char >
void v8::Utf8WriterVisitor::Visit ( const Char *  chars,
const int  length 
)
inline

Definition at line 4629 of file api.cc.

4629  {
4630  using namespace unibrow;
4632  if (length == 0) return;
4633  // Copy state to stack.
4634  char* buffer = buffer_;
4635  int last_character =
4636  sizeof(Char) == 1 ? Utf16::kNoPreviousCharacter : last_character_;
4637  int i = 0;
4638  // Do a fast loop where there is no exit capacity check.
4639  while (true) {
4640  int fast_length;
4641  if (skip_capacity_check_) {
4642  fast_length = length;
4643  } else {
4644  int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
4645  // Need enough space to write everything but one character.
4646  STATIC_ASSERT(Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit == 3);
4647  int max_size_per_char = sizeof(Char) == 1 ? 2 : 3;
4648  int writable_length =
4649  (remaining_capacity - max_size_per_char)/max_size_per_char;
4650  // Need to drop into slow loop.
4651  if (writable_length <= 0) break;
4652  fast_length = i + writable_length;
4653  if (fast_length > length) fast_length = length;
4654  }
4655  // Write the characters to the stream.
4656  if (sizeof(Char) == 1) {
4657  for (; i < fast_length; i++) {
4658  buffer +=
4659  Utf8::EncodeOneByte(buffer, static_cast<uint8_t>(*chars++));
4660  DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
4661  }
4662  } else {
4663  for (; i < fast_length; i++) {
4664  uint16_t character = *chars++;
4665  buffer += Utf8::Encode(buffer,
4666  character,
4667  last_character,
4669  last_character = character;
4670  DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
4671  }
4672  }
4673  // Array is fully written. Exit.
4674  if (fast_length == length) {
4675  // Write state back out to object.
4676  last_character_ = last_character;
4677  buffer_ = buffer;
4678  utf16_chars_read_ += length;
4679  return;
4680  }
4681  }
4683  // Slow loop. Must check capacity on each iteration.
4684  int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
4685  DCHECK(remaining_capacity >= 0);
4686  for (; i < length && remaining_capacity > 0; i++) {
4687  uint16_t character = *chars++;
4688  // remaining_capacity is <= 3 bytes at this point, so we do not write out
4689  // an umatched lead surrogate.
4690  if (replace_invalid_utf8_ && Utf16::IsLeadSurrogate(character)) {
4691  early_termination_ = true;
4692  break;
4693  }
4694  int written = WriteEndCharacter(character,
4695  last_character,
4696  remaining_capacity,
4697  buffer,
4699  if (written == 0) {
4700  early_termination_ = true;
4701  break;
4702  }
4703  buffer += written;
4704  remaining_capacity -= written;
4705  last_character = character;
4706  }
4707  // Write state back out to object.
4708  last_character_ = last_character;
4709  buffer_ = buffer;
4710  utf16_chars_read_ += i;
4711  }
static int WriteEndCharacter(uint16_t character, int last_character, int remaining, char *const buffer, bool replace_invalid_utf8)
Definition: api.cc:4584
#define DCHECK(condition)
Definition: logging.h:205
#define STATIC_ASSERT(test)
Definition: macros.h:311
unsigned short uint16_t
Definition: unicode.cc:23

References DCHECK, and STATIC_ASSERT.

◆ VisitOneByteString()

void v8::Utf8WriterVisitor::VisitOneByteString ( const uint8_t *  chars,
int  length 
)
inline

Definition at line 4717 of file api.cc.

4717  {
4718  Visit(chars, length);
4719  }
void Visit(const Char *chars, const int length)
Definition: api.cc:4629

◆ VisitTwoByteString()

void v8::Utf8WriterVisitor::VisitTwoByteString ( const uint16_t *  chars,
int  length 
)
inline

Definition at line 4721 of file api.cc.

4721  {
4722  Visit(chars, length);
4723  }

◆ WriteEndCharacter()

static int v8::Utf8WriterVisitor::WriteEndCharacter ( uint16_t  character,
int  last_character,
int  remaining,
char *const  buffer,
bool  replace_invalid_utf8 
)
inlinestatic

Definition at line 4584 of file api.cc.

4588  {
4589  using namespace unibrow;
4590  DCHECK(remaining > 0);
4591  // We can't use a local buffer here because Encode needs to modify
4592  // previous characters in the stream. We know, however, that
4593  // exactly one character will be advanced.
4594  if (Utf16::IsSurrogatePair(last_character, character)) {
4595  int written = Utf8::Encode(buffer,
4596  character,
4597  last_character,
4598  replace_invalid_utf8);
4599  DCHECK(written == 1);
4600  return written;
4601  }
4602  // Use a scratch buffer to check the required characters.
4603  char temp_buffer[Utf8::kMaxEncodedSize];
4604  // Can't encode using last_character as gcc has array bounds issues.
4605  int written = Utf8::Encode(temp_buffer,
4606  character,
4607  Utf16::kNoPreviousCharacter,
4608  replace_invalid_utf8);
4609  // Won't fit.
4610  if (written > remaining) return 0;
4611  // Copy over the character from temp_buffer.
4612  for (int j = 0; j < written; j++) {
4613  buffer[j] = temp_buffer[j];
4614  }
4615  return written;
4616  }

References DCHECK.

Member Data Documentation

◆ buffer_

char* v8::Utf8WriterVisitor::buffer_
private

Definition at line 4742 of file api.cc.

◆ capacity_

int v8::Utf8WriterVisitor::capacity_
private

Definition at line 4744 of file api.cc.

◆ early_termination_

bool v8::Utf8WriterVisitor::early_termination_
private

Definition at line 4740 of file api.cc.

◆ last_character_

int v8::Utf8WriterVisitor::last_character_
private

Definition at line 4741 of file api.cc.

◆ replace_invalid_utf8_

bool const v8::Utf8WriterVisitor::replace_invalid_utf8_
private

Definition at line 4746 of file api.cc.

◆ skip_capacity_check_

bool const v8::Utf8WriterVisitor::skip_capacity_check_
private

Definition at line 4745 of file api.cc.

◆ start_

char* const v8::Utf8WriterVisitor::start_
private

Definition at line 4743 of file api.cc.

◆ utf16_chars_read_

int v8::Utf8WriterVisitor::utf16_chars_read_
private

Definition at line 4747 of file api.cc.


The documentation for this class was generated from the following file: