2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-03-31 12:41:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A class which represents a fragment of text (eg inside a text
|
|
|
|
* node); if only codepoints below 256 are used, the text is stored as
|
2014-01-04 19:02:17 +04:00
|
|
|
* a char*; otherwise the text is stored as a char16_t*
|
2006-03-31 12:41:49 +04:00
|
|
|
*/
|
|
|
|
|
2004-12-08 04:50:33 +03:00
|
|
|
#include "nsTextFragment.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2008-02-03 02:41:24 +03:00
|
|
|
#include "nsMemory.h"
|
2004-12-08 04:50:33 +03:00
|
|
|
#include "nsBidiUtils.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
2008-09-11 07:22:23 +04:00
|
|
|
#include "nsUTF8Utils.h"
|
2015-07-07 21:32:25 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2010-08-10 02:32:18 +04:00
|
|
|
#include "mozilla/SSE.h"
|
2012-10-02 23:14:50 +04:00
|
|
|
#include "nsTextFragmentImpl.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
#define TEXTFRAG_WHITE_AFTER_NEWLINE 50
|
|
|
|
#define TEXTFRAG_MAX_NEWLINES 7
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Static buffer used for common fragments
|
|
|
|
static char* sSpaceSharedString[TEXTFRAG_MAX_NEWLINES + 1];
|
|
|
|
static char* sTabSharedString[TEXTFRAG_MAX_NEWLINES + 1];
|
|
|
|
static char sSingleCharSharedString[256];
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2015-07-07 21:32:25 +03:00
|
|
|
using mozilla::CheckedUint32;
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsTextFragment::Init()
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
2006-03-24 06:29:52 +03:00
|
|
|
// Create whitespace strings
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2006-03-24 06:29:52 +03:00
|
|
|
for (i = 0; i <= TEXTFRAG_MAX_NEWLINES; ++i) {
|
|
|
|
sSpaceSharedString[i] = new char[1 + i + TEXTFRAG_WHITE_AFTER_NEWLINE];
|
|
|
|
sTabSharedString[i] = new char[1 + i + TEXTFRAG_WHITE_AFTER_NEWLINE];
|
|
|
|
sSpaceSharedString[i][0] = ' ';
|
|
|
|
sTabSharedString[i][0] = ' ';
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t j;
|
2006-03-24 06:29:52 +03:00
|
|
|
for (j = 1; j < 1 + i; ++j) {
|
|
|
|
sSpaceSharedString[i][j] = '\n';
|
|
|
|
sTabSharedString[i][j] = '\n';
|
|
|
|
}
|
|
|
|
for (; j < (1 + i + TEXTFRAG_WHITE_AFTER_NEWLINE); ++j) {
|
|
|
|
sSpaceSharedString[i][j] = ' ';
|
|
|
|
sTabSharedString[i][j] = '\t';
|
|
|
|
}
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Create single-char strings
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
sSingleCharSharedString[i] = i;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
return NS_OK;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsTextFragment::Shutdown()
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2006-03-24 06:29:52 +03:00
|
|
|
for (i = 0; i <= TEXTFRAG_MAX_NEWLINES; ++i) {
|
|
|
|
delete [] sSpaceSharedString[i];
|
|
|
|
delete [] sTabSharedString[i];
|
2012-07-30 18:20:58 +04:00
|
|
|
sSpaceSharedString[i] = nullptr;
|
|
|
|
sTabSharedString[i] = nullptr;
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
nsTextFragment::~nsTextFragment()
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
2006-03-24 06:29:52 +03:00
|
|
|
ReleaseText();
|
2009-03-08 22:01:02 +03:00
|
|
|
MOZ_COUNT_DTOR(nsTextFragment);
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
void
|
|
|
|
nsTextFragment::ReleaseText()
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
2006-03-24 06:29:52 +03:00
|
|
|
if (mState.mLength && m1b && mState.mInHeap) {
|
2015-02-19 07:51:06 +03:00
|
|
|
free(m2b); // m1b == m2b as far as free is concerned
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
m1b = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mIsBidi = false;
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Set mState.mIs2b, mState.mInHeap, and mState.mLength = 0 with mAllBits;
|
|
|
|
mAllBits = 0;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTextFragment&
|
2006-03-24 06:29:52 +03:00
|
|
|
nsTextFragment::operator=(const nsTextFragment& aOther)
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
|
|
|
ReleaseText();
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
if (aOther.mState.mLength) {
|
|
|
|
if (!aOther.mState.mInHeap) {
|
|
|
|
m1b = aOther.m1b; // This will work even if aOther is using m2b
|
|
|
|
}
|
|
|
|
else {
|
2015-07-07 21:32:25 +03:00
|
|
|
CheckedUint32 m2bSize = aOther.mState.mLength;
|
|
|
|
m2bSize *= (aOther.mState.mIs2b ? sizeof(char16_t) : sizeof(char));
|
|
|
|
m2b = nullptr;
|
|
|
|
if (m2bSize.isValid()) {
|
|
|
|
m2b = static_cast<char16_t*>(malloc(m2bSize.value()));
|
|
|
|
}
|
2013-12-19 18:09:00 +04:00
|
|
|
|
|
|
|
if (m2b) {
|
2015-07-07 21:32:25 +03:00
|
|
|
memcpy(m2b, aOther.m2b, m2bSize.value());
|
2013-12-19 18:09:00 +04:00
|
|
|
} else {
|
|
|
|
// allocate a buffer for a single REPLACEMENT CHARACTER
|
2014-01-04 19:02:17 +04:00
|
|
|
m2b = static_cast<char16_t*>(moz_xmalloc(sizeof(char16_t)));
|
2013-12-19 18:09:00 +04:00
|
|
|
m2b[0] = 0xFFFD; // REPLACEMENT CHARACTER
|
|
|
|
mState.mIs2b = true;
|
|
|
|
mState.mInHeap = true;
|
|
|
|
mState.mLength = 1;
|
|
|
|
}
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
if (m1b) {
|
|
|
|
mAllBits = aOther.mAllBits;
|
|
|
|
}
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static inline int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
FirstNon8BitUnvectorized(const char16_t *str, const char16_t *end)
|
2010-08-10 02:32:18 +04:00
|
|
|
{
|
2012-10-02 23:14:50 +04:00
|
|
|
typedef Non8BitParameters<sizeof(size_t)> p;
|
|
|
|
const size_t mask = p::mask();
|
|
|
|
const uint32_t alignMask = p::alignMask();
|
|
|
|
const uint32_t numUnicharsPerWord = p::numUnicharsPerWord();
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t len = end - str;
|
|
|
|
int32_t i = 0;
|
2010-08-10 02:32:18 +04:00
|
|
|
|
|
|
|
// Align ourselves to a word boundary.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t alignLen =
|
2014-01-04 19:02:17 +04:00
|
|
|
std::min(len, int32_t(((-NS_PTR_TO_INT32(str)) & alignMask) / sizeof(char16_t)));
|
2010-08-10 02:32:18 +04:00
|
|
|
for (; i < alignLen; i++) {
|
|
|
|
if (str[i] > 255)
|
2011-09-09 20:27:00 +04:00
|
|
|
return i;
|
2010-08-10 02:32:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check one word at a time.
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t wordWalkEnd = ((len - i) / numUnicharsPerWord) * numUnicharsPerWord;
|
2010-08-10 02:32:18 +04:00
|
|
|
for (; i < wordWalkEnd; i += numUnicharsPerWord) {
|
|
|
|
const size_t word = *reinterpret_cast<const size_t*>(str + i);
|
|
|
|
if (word & mask)
|
2011-09-09 20:27:00 +04:00
|
|
|
return i;
|
2010-08-10 02:32:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Take care of the remainder one character at a time.
|
|
|
|
for (; i < len; i++) {
|
|
|
|
if (str[i] > 255)
|
2011-09-09 20:27:00 +04:00
|
|
|
return i;
|
2010-08-10 02:32:18 +04:00
|
|
|
}
|
|
|
|
|
2011-09-09 20:27:00 +04:00
|
|
|
return -1;
|
2010-08-10 02:32:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZILLA_MAY_SUPPORT_SSE2
|
|
|
|
namespace mozilla {
|
|
|
|
namespace SSE2 {
|
2014-01-04 19:02:17 +04:00
|
|
|
int32_t FirstNon8Bit(const char16_t *str, const char16_t *end);
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace SSE2
|
|
|
|
} // namespace mozilla
|
2010-08-10 02:32:18 +04:00
|
|
|
#endif
|
|
|
|
|
2011-09-09 20:27:00 +04:00
|
|
|
/*
|
|
|
|
* This function returns -1 if all characters in str are 8 bit characters.
|
|
|
|
* Otherwise, it returns a value less than or equal to the index of the first
|
|
|
|
* non-8bit character in str. For example, if first non-8bit character is at
|
|
|
|
* position 25, it may return 25, or for example 24, or 16. But it guarantees
|
|
|
|
* there is no non-8bit character before returned value.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static inline int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
FirstNon8Bit(const char16_t *str, const char16_t *end)
|
2010-08-10 02:32:18 +04:00
|
|
|
{
|
|
|
|
#ifdef MOZILLA_MAY_SUPPORT_SSE2
|
|
|
|
if (mozilla::supports_sse2()) {
|
2011-09-09 20:27:00 +04:00
|
|
|
return mozilla::SSE2::FirstNon8Bit(str, end);
|
2010-08-10 02:32:18 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-09 20:27:00 +04:00
|
|
|
return FirstNon8BitUnvectorized(str, end);
|
2010-08-10 02:32:18 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 18:09:00 +04:00
|
|
|
bool
|
2014-01-04 19:02:17 +04:00
|
|
|
nsTextFragment::SetTo(const char16_t* aBuffer, int32_t aLength, bool aUpdateBidi)
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
|
|
|
ReleaseText();
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
if (aLength == 0) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return true;
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t firstChar = *aBuffer;
|
2006-03-24 06:29:52 +03:00
|
|
|
if (aLength == 1 && firstChar < 256) {
|
|
|
|
m1b = sSingleCharSharedString + firstChar;
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mInHeap = false;
|
|
|
|
mState.mIs2b = false;
|
2006-03-24 06:29:52 +03:00
|
|
|
mState.mLength = 1;
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2013-12-19 18:09:00 +04:00
|
|
|
return true;
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *ucp = aBuffer;
|
|
|
|
const char16_t *uend = aBuffer + aLength;
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Check if we can use a shared string
|
2011-03-30 21:50:44 +04:00
|
|
|
if (aLength <= 1 + TEXTFRAG_WHITE_AFTER_NEWLINE + TEXTFRAG_MAX_NEWLINES &&
|
|
|
|
(firstChar == ' ' || firstChar == '\n' || firstChar == '\t')) {
|
2006-03-24 06:29:52 +03:00
|
|
|
if (firstChar == ' ') {
|
|
|
|
++ucp;
|
|
|
|
}
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* start = ucp;
|
2006-03-24 06:29:52 +03:00
|
|
|
while (ucp < uend && *ucp == '\n') {
|
|
|
|
++ucp;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* endNewLine = ucp;
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t space = ucp < uend && *ucp == '\t' ? '\t' : ' ';
|
2006-03-24 06:29:52 +03:00
|
|
|
while (ucp < uend && *ucp == space) {
|
|
|
|
++ucp;
|
|
|
|
}
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
if (ucp == uend &&
|
|
|
|
endNewLine - start <= TEXTFRAG_MAX_NEWLINES &&
|
|
|
|
ucp - endNewLine <= TEXTFRAG_WHITE_AFTER_NEWLINE) {
|
|
|
|
char** strings = space == ' ' ? sSpaceSharedString : sTabSharedString;
|
|
|
|
m1b = strings[endNewLine - start];
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// If we didn't find a space in the beginning, skip it now.
|
|
|
|
if (firstChar != ' ') {
|
|
|
|
++m1b;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mInHeap = false;
|
|
|
|
mState.mIs2b = false;
|
2004-12-08 04:50:33 +03:00
|
|
|
mState.mLength = aLength;
|
2006-03-24 06:29:52 +03:00
|
|
|
|
2013-12-19 18:09:00 +04:00
|
|
|
return true;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 00:04:59 +03:00
|
|
|
// See if we need to store the data in ucs2 or not
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t first16bit = FirstNon8Bit(ucp, uend);
|
2004-12-08 04:50:33 +03:00
|
|
|
|
2011-09-09 20:27:00 +04:00
|
|
|
if (first16bit != -1) { // aBuffer contains no non-8bit character
|
2006-03-24 06:29:52 +03:00
|
|
|
// Use ucs2 storage because we have to
|
2015-07-07 21:32:25 +03:00
|
|
|
CheckedUint32 m2bSize = aLength;
|
|
|
|
m2bSize *= sizeof(char16_t);
|
|
|
|
if (!m2bSize.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m2b = static_cast<char16_t*>(malloc(m2bSize.value()));
|
2006-03-24 06:29:52 +03:00
|
|
|
if (!m2b) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return false;
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
2015-07-07 21:32:25 +03:00
|
|
|
memcpy(m2b, aBuffer, m2bSize.value());
|
2011-09-09 20:27:00 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mIs2b = true;
|
2011-09-09 20:27:00 +04:00
|
|
|
if (aUpdateBidi) {
|
|
|
|
UpdateBidiFlag(aBuffer + first16bit, aLength - first16bit);
|
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
} else {
|
|
|
|
// Use 1 byte storage because we can
|
2015-07-07 21:32:25 +03:00
|
|
|
char* buff = static_cast<char*>(malloc(aLength));
|
2006-03-24 06:29:52 +03:00
|
|
|
if (!buff) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return false;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Copy data
|
2010-08-13 20:15:44 +04:00
|
|
|
LossyConvertEncoding16to8 converter(buff);
|
2008-09-11 07:22:23 +04:00
|
|
|
copy_string(aBuffer, aBuffer+aLength, converter);
|
2006-03-24 06:29:52 +03:00
|
|
|
m1b = buff;
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mIs2b = false;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
2006-03-24 06:29:52 +03:00
|
|
|
|
|
|
|
// Setup our fields
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mInHeap = true;
|
2006-03-24 06:29:52 +03:00
|
|
|
mState.mLength = aLength;
|
2013-12-19 18:09:00 +04:00
|
|
|
|
|
|
|
return true;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-04 19:02:17 +04:00
|
|
|
nsTextFragment::CopyTo(char16_t *aDest, int32_t aOffset, int32_t aCount)
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aOffset >= 0, "Bad offset passed to nsTextFragment::CopyTo()!");
|
|
|
|
NS_ASSERTION(aCount >= 0, "Bad count passed to nsTextFragment::CopyTo()!");
|
|
|
|
|
|
|
|
if (aOffset < 0) {
|
|
|
|
aOffset = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if (uint32_t(aOffset + aCount) > GetLength()) {
|
2004-12-08 04:50:33 +03:00
|
|
|
aCount = mState.mLength - aOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCount != 0) {
|
|
|
|
if (mState.mIs2b) {
|
2014-01-04 19:02:17 +04:00
|
|
|
memcpy(aDest, m2b + aOffset, sizeof(char16_t) * aCount);
|
2004-12-08 04:50:33 +03:00
|
|
|
} else {
|
2006-03-24 06:29:52 +03:00
|
|
|
const char *cp = m1b + aOffset;
|
|
|
|
const char *end = cp + aCount;
|
2010-08-13 20:15:44 +04:00
|
|
|
LossyConvertEncoding8to16 converter(aDest);
|
|
|
|
copy_string(cp, end, converter);
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:09:00 +04:00
|
|
|
bool
|
2014-01-04 19:02:17 +04:00
|
|
|
nsTextFragment::Append(const char16_t* aBuffer, uint32_t aLength, bool aUpdateBidi)
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
2006-03-24 06:29:52 +03:00
|
|
|
// This is a common case because some callsites create a textnode
|
|
|
|
// with a value by creating the node and then calling AppendData.
|
|
|
|
if (mState.mLength == 0) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return SetTo(aBuffer, aLength, aUpdateBidi);
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Should we optimize for aData.Length() == 0?
|
|
|
|
|
2015-07-07 21:32:25 +03:00
|
|
|
CheckedUint32 length = mState.mLength;
|
|
|
|
length += aLength;
|
|
|
|
|
|
|
|
if (!length.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
if (mState.mIs2b) {
|
2015-07-07 21:32:25 +03:00
|
|
|
length *= sizeof(char16_t);
|
|
|
|
if (!length.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Already a 2-byte string so the result will be too
|
2015-07-07 21:32:25 +03:00
|
|
|
char16_t* buff = static_cast<char16_t*>(realloc(m2b, length.value()));
|
2006-03-24 06:29:52 +03:00
|
|
|
if (!buff) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return false;
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
2011-09-09 20:27:00 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
memcpy(buff + mState.mLength, aBuffer, aLength * sizeof(char16_t));
|
2006-10-28 05:22:53 +04:00
|
|
|
mState.mLength += aLength;
|
2006-03-24 06:29:52 +03:00
|
|
|
m2b = buff;
|
2008-02-03 02:41:24 +03:00
|
|
|
|
2011-09-09 20:27:00 +04:00
|
|
|
if (aUpdateBidi) {
|
|
|
|
UpdateBidiFlag(aBuffer, aLength);
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:09:00 +04:00
|
|
|
return true;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// Current string is a 1-byte string, check if the new data fits in one byte too.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t first16bit = FirstNon8Bit(aBuffer, aBuffer + aLength);
|
2006-03-24 06:29:52 +03:00
|
|
|
|
2011-09-09 20:27:00 +04:00
|
|
|
if (first16bit != -1) { // aBuffer contains no non-8bit character
|
2015-07-07 21:32:25 +03:00
|
|
|
length *= sizeof(char16_t);
|
|
|
|
if (!length.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-24 06:29:52 +03:00
|
|
|
// The old data was 1-byte, but the new is not so we have to expand it
|
|
|
|
// all to 2-byte
|
2015-07-07 21:32:25 +03:00
|
|
|
char16_t* buff = static_cast<char16_t*>(malloc(length.value()));
|
2006-03-24 06:29:52 +03:00
|
|
|
if (!buff) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return false;
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
|
|
|
|
2010-08-13 20:15:44 +04:00
|
|
|
// Copy data into buff
|
|
|
|
LossyConvertEncoding8to16 converter(buff);
|
|
|
|
copy_string(m1b, m1b+mState.mLength, converter);
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
memcpy(buff + mState.mLength, aBuffer, aLength * sizeof(char16_t));
|
2007-12-15 12:45:33 +03:00
|
|
|
mState.mLength += aLength;
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mIs2b = true;
|
2008-02-03 02:41:24 +03:00
|
|
|
|
|
|
|
if (mState.mInHeap) {
|
2015-02-19 07:51:06 +03:00
|
|
|
free(m2b);
|
2008-02-03 02:41:24 +03:00
|
|
|
}
|
2006-03-24 06:29:52 +03:00
|
|
|
m2b = buff;
|
2008-02-03 02:41:24 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mInHeap = true;
|
2006-03-24 06:29:52 +03:00
|
|
|
|
2011-09-09 20:27:00 +04:00
|
|
|
if (aUpdateBidi) {
|
|
|
|
UpdateBidiFlag(aBuffer + first16bit, aLength - first16bit);
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:09:00 +04:00
|
|
|
return true;
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The new and the old data is all 1-byte
|
2008-02-03 02:41:24 +03:00
|
|
|
char* buff;
|
|
|
|
if (mState.mInHeap) {
|
2015-07-07 21:32:25 +03:00
|
|
|
buff = static_cast<char*>(realloc(const_cast<char*>(m1b), length.value()));
|
2008-02-03 02:41:24 +03:00
|
|
|
if (!buff) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return false;
|
2008-02-03 02:41:24 +03:00
|
|
|
}
|
2006-03-24 06:29:52 +03:00
|
|
|
}
|
2008-02-03 02:41:24 +03:00
|
|
|
else {
|
2015-07-07 21:32:25 +03:00
|
|
|
buff = static_cast<char*>(malloc(length.value()));
|
2008-02-03 02:41:24 +03:00
|
|
|
if (!buff) {
|
2013-12-19 18:09:00 +04:00
|
|
|
return false;
|
2008-02-03 02:41:24 +03:00
|
|
|
}
|
2006-03-24 06:29:52 +03:00
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
memcpy(buff, m1b, mState.mLength);
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mInHeap = true;
|
2008-02-03 02:41:24 +03:00
|
|
|
}
|
2010-08-13 20:15:44 +04:00
|
|
|
|
|
|
|
// Copy aBuffer into buff.
|
|
|
|
LossyConvertEncoding16to8 converter(buff + mState.mLength);
|
|
|
|
copy_string(aBuffer, aBuffer + aLength, converter);
|
2006-03-24 06:29:52 +03:00
|
|
|
|
|
|
|
m1b = buff;
|
2006-10-28 05:22:53 +04:00
|
|
|
mState.mLength += aLength;
|
2006-03-24 06:29:52 +03:00
|
|
|
|
2013-12-19 18:09:00 +04:00
|
|
|
return true;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
/* virtual */ size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
nsTextFragment::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-02-02 01:58:01 +04:00
|
|
|
{
|
|
|
|
if (Is2b()) {
|
|
|
|
return aMallocSizeOf(m2b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState.mInHeap) {
|
|
|
|
return aMallocSizeOf(m1b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-08 04:50:33 +03:00
|
|
|
// To save time we only do this when we really want to know, not during
|
|
|
|
// every allocation
|
|
|
|
void
|
2014-01-04 19:02:17 +04:00
|
|
|
nsTextFragment::UpdateBidiFlag(const char16_t* aBuffer, uint32_t aLength)
|
2004-12-08 04:50:33 +03:00
|
|
|
{
|
|
|
|
if (mState.mIs2b && !mState.mIsBidi) {
|
2017-04-27 01:25:48 +03:00
|
|
|
if (HasRTLChars(aBuffer, aLength)) {
|
|
|
|
mState.mIsBidi = true;
|
2004-12-08 04:50:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|