// helper function to perform an in-place split of a space-delimited string, // and return an array of pointers for the beginning of each segment, i.e., // aOffset[0] is the first string, aOffset[1] is the second string, etc. // Used to parse attributes like columnalign='left right', rowalign='top bottom' static void SplitString(nsString& aString, // [IN/OUT] nsVoidArray& aOffset) // [OUT] { static const PRUnichar kNullCh = PRUnichar('\0'); aString.Append(kNullCh); // put an extra null at the end PRUnichar* start = aString.BeginWriting(); PRUnichar* end = start; while (kNullCh != *start) { while ((kNullCh != *start) && nsCRT::IsAsciiSpace(*start)) { // skip leading space start++; } end = start; while ((kNullCh != *end) && (PR_FALSE == nsCRT::IsAsciiSpace(*end))) { // look for space or end end++; } *end = kNullCh; // end string here if (start < end) { aOffset.AppendElement(start); // record the beginning of this segment } start = ++end; } }
void NormalizeFilePath() { #if defined(XP_WIN) PRUnichar* cur = mPath.BeginWriting(); PRUnichar* end = mPath.EndWriting(); for (; cur < end; ++cur) { if (PRUnichar('\\') == *cur) *cur = PRUnichar('/'); } #endif }
void AndroidGeckoEvent::ReadStringFromJString(nsString &aString, JNIEnv *jenv, jstring s) { if (!s) { aString.SetIsVoid(true); return; } int len = jenv->GetStringLength(s); aString.SetLength(len); jenv->GetStringRegion(s, 0, len, reinterpret_cast<jchar*>(aString.BeginWriting())); }
bool DriveMapping::DoMapping() { wchar_t drvTemplate[] = L" :"; NETRESOURCEW netRes = {0}; netRes.dwType = RESOURCETYPE_DISK; netRes.lpLocalName = drvTemplate; netRes.lpRemoteName = mRemoteUNCPath.BeginWriting(); wchar_t driveLetter = L'D'; DWORD result = NO_ERROR; do { drvTemplate[0] = driveLetter; result = WNetAddConnection2W(&netRes, nullptr, nullptr, CONNECT_TEMPORARY); } while (result == ERROR_ALREADY_ASSIGNED && ++driveLetter <= L'Z'); if (result != NO_ERROR) { return false; } mDriveLetter = driveLetter; return true; }
// Helper functions for structured cloning inline bool ReadString(JSStructuredCloneReader* aReader, nsString& aString) { MOZ_ASSERT(aReader); bool read; uint32_t nameLength, zero; read = JS_ReadUint32Pair(aReader, &nameLength, &zero); if (!read) { return false; } MOZ_ASSERT(zero == 0); aString.SetLength(nameLength); size_t charSize = sizeof(nsString::char_type); read = JS_ReadBytes(aReader, (void*) aString.BeginWriting(), nameLength * charSize); if (!read) { return false; } return true; }
/*---------------------------------------------------------------------------- ConvertStringLineBreaks ----------------------------------------------------------------------------*/ nsresult nsLinebreakConverter::ConvertStringLineBreaks(nsString& aIoString, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks) { NS_ASSERTION(aDestBreaks != eLinebreakAny && aSrcBreaks != eLinebreakSpace, "Invalid parameter"); // nothing to do if (aIoString.IsEmpty()) { return NS_OK; } nsresult rv; // remember the old buffer in case // we blow it away later nsString::char_iterator stringBuf; aIoString.BeginWriting(stringBuf); int32_t newLen; rv = ConvertUnicharLineBreaksInSitu(&stringBuf, aSrcBreaks, aDestBreaks, aIoString.Length() + 1, &newLen); if (NS_FAILED(rv)) { return rv; } if (stringBuf != aIoString.get()) { aIoString.Adopt(stringBuf); } return NS_OK; }
/* static */ nsresult nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData, PRUint32 aLength, const nsString& aHintCharset, nsIDocument* aDocument, nsString& aString) { if (!aLength) { aString.Truncate(); return NS_OK; } nsCAutoString characterSet; nsresult rv = NS_OK; if (aChannel) { rv = aChannel->GetContentCharset(characterSet); } if (!aHintCharset.IsEmpty() && (NS_FAILED(rv) || characterSet.IsEmpty())) { // charset name is always ASCII. LossyCopyUTF16toASCII(aHintCharset, characterSet); } if (NS_FAILED(rv) || characterSet.IsEmpty()) { DetectByteOrderMark(aData, aLength, characterSet); } if (characterSet.IsEmpty()) { // charset from document default characterSet = aDocument->GetDocumentCharacterSet(); } if (characterSet.IsEmpty()) { // fall back to ISO-8859-1, see bug 118404 characterSet.AssignLiteral("ISO-8859-1"); } nsCOMPtr<nsICharsetConverterManager> charsetConv = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder; if (NS_SUCCEEDED(rv) && charsetConv) { rv = charsetConv->GetUnicodeDecoder(characterSet.get(), getter_AddRefs(unicodeDecoder)); if (NS_FAILED(rv)) { // fall back to ISO-8859-1 if charset is not supported. (bug 230104) rv = charsetConv->GetUnicodeDecoderRaw("ISO-8859-1", getter_AddRefs(unicodeDecoder)); } } // converts from the charset to unicode if (NS_SUCCEEDED(rv)) { PRInt32 unicodeLength = 0; rv = unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData), aLength, &unicodeLength); if (NS_SUCCEEDED(rv)) { if (!EnsureStringLength(aString, unicodeLength)) return NS_ERROR_OUT_OF_MEMORY; PRUnichar *ustr = aString.BeginWriting(); PRInt32 consumedLength = 0; PRInt32 originalLength = aLength; PRInt32 convertedLength = 0; PRInt32 bufferLength = unicodeLength; do { rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData), (PRInt32 *) &aLength, ustr, &unicodeLength); if (NS_FAILED(rv)) { // if we failed, we consume one byte, replace it with U+FFFD // and try the conversion again. ustr[unicodeLength++] = (PRUnichar)0xFFFD; ustr += unicodeLength; unicodeDecoder->Reset(); } aData += ++aLength; consumedLength += aLength; aLength = originalLength - consumedLength; convertedLength += unicodeLength; unicodeLength = bufferLength - convertedLength; } while (NS_FAILED(rv) && (originalLength > consumedLength) && (bufferLength > convertedLength)); aString.SetLength(convertedLength); } } return rv; }
static bool SetOperator(OperatorData* aOperatorData, nsOperatorFlags aForm, const nsCString& aOperator, nsString& aAttributes) { static const char16_t kNullCh = char16_t('\0'); // aOperator is in the expanded format \uNNNN\uNNNN ... // First compress these Unicode points to the internal nsString format int32_t i = 0; nsAutoString name, value; int32_t len = aOperator.Length(); char16_t c = aOperator[i++]; uint32_t state = 0; char16_t uchar = 0; while (i <= len) { if (0 == state) { if (c != '\\') return false; if (i < len) c = aOperator[i]; i++; if (('u' != c) && ('U' != c)) return false; if (i < len) c = aOperator[i]; i++; state++; } else { if (('0' <= c) && (c <= '9')) uchar = (uchar << 4) | (c - '0'); else if (('a' <= c) && (c <= 'f')) uchar = (uchar << 4) | (c - 'a' + 0x0a); else if (('A' <= c) && (c <= 'F')) uchar = (uchar << 4) | (c - 'A' + 0x0a); else return false; if (i < len) c = aOperator[i]; i++; state++; if (5 == state) { value.Append(uchar); uchar = 0; state = 0; } } } if (0 != state) return false; // Quick return when the caller doesn't care about the attributes and just wants // to know if this is a valid operator (this is the case at the first pass of the // parsing of the dictionary in InitOperators()) if (!aForm) return true; // Add operator to hash table aOperatorData->mFlags |= aForm; aOperatorData->mStr.Assign(value); value.AppendInt(aForm, 10); gOperatorTable->Put(value, aOperatorData); #ifdef DEBUG NS_LossyConvertUTF16toASCII str(aAttributes); #endif // Loop over the space-delimited list of attributes to get the name:value pairs aAttributes.Append(kNullCh); // put an extra null at the end char16_t* start = aAttributes.BeginWriting(); char16_t* end = start; while ((kNullCh != *start) && (kDashCh != *start)) { name.SetLength(0); value.SetLength(0); // skip leading space, the dash amounts to the end of the line while ((kNullCh!=*start) && (kDashCh!=*start) && nsCRT::IsAsciiSpace(*start)) { ++start; } end = start; // look for ':' while ((kNullCh!=*end) && (kDashCh!=*end) && !nsCRT::IsAsciiSpace(*end) && (kColonCh!=*end)) { ++end; } // If ':' is not found, then it's a boolean property bool IsBooleanProperty = (kColonCh != *end); *end = kNullCh; // end segment here // this segment is the name if (start < end) { name.Assign(start); } if (IsBooleanProperty) { SetBooleanProperty(aOperatorData, name); } else { start = ++end; // look for space or end of line while ((kNullCh!=*end) && (kDashCh!=*end) && !nsCRT::IsAsciiSpace(*end)) { ++end; } *end = kNullCh; // end segment here if (start < end) { // this segment is the value value.Assign(start); } SetProperty(aOperatorData, name, value); } start = ++end; } return true; }
PRBool SetOperator(OperatorData* aOperatorData, nsOperatorFlags aForm, const nsCString& aOperator, nsString& aAttributes) { // aOperator is in the expanded format \uNNNN\uNNNN ... // First compress these Unicode points to the internal nsString format PRInt32 i = 0; nsAutoString name, value; PRInt32 len = aOperator.Length(); PRUnichar c = aOperator[i++]; PRUint32 state = 0; PRUnichar uchar = 0; while (i <= len) { if (0 == state) { if (c != '\\') return PR_FALSE; if (i < len) c = aOperator[i]; i++; if (('u' != c) && ('U' != c)) return PR_FALSE; if (i < len) c = aOperator[i]; i++; state++; } else { if (('0' <= c) && (c <= '9')) uchar = (uchar << 4) | (c - '0'); else if (('a' <= c) && (c <= 'f')) uchar = (uchar << 4) | (c - 'a' + 0x0a); else if (('A' <= c) && (c <= 'F')) uchar = (uchar << 4) | (c - 'A' + 0x0a); else return PR_FALSE; if (i < len) c = aOperator[i]; i++; state++; if (5 == state) { value.Append(uchar); uchar = 0; state = 0; } } } if (0 != state) return PR_FALSE; // Quick return when the caller doesn't care about the attributes and just wants // to know if this is a valid operator (this is the case at the first pass of the // parsing of the dictionary in InitOperators()) if (!aForm) return PR_TRUE; // Add operator to hash table (symmetric="true" by default for all operators) aOperatorData->mFlags |= aForm | NS_MATHML_OPERATOR_SYMMETRIC; aOperatorData->mStr.Assign(value); value.AppendInt(aForm, 10); nsStringKey key(value); gOperatorTable->Put(&key, aOperatorData); #ifdef NS_DEBUG NS_LossyConvertUTF16toASCII str(aAttributes); #endif // Loop over the space-delimited list of attributes to get the name:value pairs aAttributes.Append(kNullCh); // put an extra null at the end PRUnichar* start = aAttributes.BeginWriting(); PRUnichar* end = start; while ((kNullCh != *start) && (kDashCh != *start)) { name.SetLength(0); value.SetLength(0); // skip leading space, the dash amounts to the end of the line while ((kNullCh!=*start) && (kDashCh!=*start) && nsCRT::IsAsciiSpace(*start)) { ++start; } end = start; // look for ':' or '=' while ((kNullCh!=*end) && (kDashCh!=*end) && (kColonCh!=*end) && (kEqualCh!=*end)) { ++end; } if ((kColonCh!=*end) && (kEqualCh!=*end)) { #ifdef NS_DEBUG printf("Bad MathML operator: %s\n", str.get()); #endif return PR_TRUE; } *end = kNullCh; // end segment here // this segment is the name if (start < end) { name.Assign(start); } start = ++end; // look for space or end of line while ((kNullCh!=*end) && (kDashCh!=*start) && !nsCRT::IsAsciiSpace(*end)) { ++end; } *end = kNullCh; // end segment here // this segment is the value if (start < end) { value.Assign(start); } SetProperty(aOperatorData, name, value); start = ++end; } return PR_TRUE; }
static void ZapString(nsString &s) { ZapBuf(s.BeginWriting(), s.Length() * 2); }