static void EscapeAmpersands(nsString& aToolTip) { // First, check to see whether we have any ampersands. int32_t pos = aToolTip.FindChar('&'); if (pos == kNotFound) return; // Next, see if we only have bare ampersands. pos = MsgFind(aToolTip, "&&", false, pos); // Windows tooltip code removes one ampersand from each run, // then collapses pairs of amperands. This means that in the easy case, // we need to replace each ampersand with three. MsgReplaceSubstring(aToolTip, NS_LITERAL_STRING("&"), NS_LITERAL_STRING("&&&")); if (pos == kNotFound) return; // We inserted too many ampersands. Remove some. for (;;) { pos = MsgFind(aToolTip, "&&&&&&", false, pos); if (pos == kNotFound) return; aToolTip.Cut(pos, 1); pos += 2; } }
// The driver ID is a string like PCI\VEN_15AD&DEV_0405&SUBSYS_040515AD, possibly // followed by &REV_XXXX. We uppercase the string, and strip the &REV_ part // from it, if found. static void normalizeDriverId(nsString& driverid) { ToUpperCase(driverid); PRInt32 rev = driverid.Find(NS_LITERAL_CSTRING("&REV_")); if (rev != -1) { driverid.Cut(rev, driverid.Length()); } }
static void ParseAlignAttribute(nsString& aValue, eAlign& aAlign, PRInt32& aRowIndex) { // by default, the table is centered about the axis aRowIndex = 0; aAlign = eAlign_axis; PRInt32 len = 0; if (0 == aValue.Find("top")) { len = 3; // 3 is the length of 'top' aAlign = eAlign_top; } else if (0 == aValue.Find("bottom")) { len = 6; // 6 is the length of 'bottom' aAlign = eAlign_bottom; } else if (0 == aValue.Find("center")) { len = 6; // 6 is the length of 'center' aAlign = eAlign_center; } else if (0 == aValue.Find("baseline")) { len = 8; // 8 is the length of 'baseline' aAlign = eAlign_baseline; } else if (0 == aValue.Find("axis")) { len = 4; // 4 is the length of 'axis' aAlign = eAlign_axis; } if (len) { PRInt32 error; aValue.Cut(0, len); // aValue is not a const here aRowIndex = aValue.ToInteger(&error); if (error) aRowIndex = 0; } }
void nsEudoraCompose::ExtractCharset(nsString& str) { int32_t idx = MsgFind(str, "charset=", true, 0); if (idx != -1) { str.Cut(0, idx + 8); idx = str.FindChar(';'); if (idx != -1) str.SetLength(idx); str.Trim(kWhitespace); if ((str.CharAt(0) == '"') && (str.Length() > 2)) { str.SetLength(str.Length() - 1); str.Cut(0, 1); str.Trim(kWhitespace); } } else str.Truncate(); }
// EscapeStr takes the passed in string and // escapes it IN PLACE. void mozTXTToHTMLConv::EscapeStr(nsString& aInString, bool inAttribute) { // the replace substring routines // don't seem to work if you have a character // in the in string that is also in the replacement // string! =( //aInString.ReplaceSubstring("&", "&"); //aInString.ReplaceSubstring("<", "<"); //aInString.ReplaceSubstring(">", ">"); for (uint32_t i = 0; i < aInString.Length();) { switch (aInString[i]) { case '<': aInString.Cut(i, 1); aInString.Insert(NS_LITERAL_STRING("<"), i); i += 4; // skip past the integers we just added break; case '>': aInString.Cut(i, 1); aInString.Insert(NS_LITERAL_STRING(">"), i); i += 4; // skip past the integers we just added break; case '&': aInString.Cut(i, 1); aInString.Insert(NS_LITERAL_STRING("&"), i); i += 5; // skip past the integers we just added break; case '"': if (inAttribute) { aInString.Cut(i, 1); aInString.Insert(NS_LITERAL_STRING("""), i); i += 6; break; } // else fall through MOZ_FALLTHROUGH; default: i++; } } }
void nsEudoraCompose::ExtractType(nsString& str) { nsString tStr; int32_t idx = str.FindChar(';'); if (idx != -1) str.SetLength(idx); str.Trim(kWhitespace); if ((str.CharAt(0) == '"') && (str.Length() > 2)) { str.SetLength(str.Length() - 1); str.Cut(0, 1); str.Trim(kWhitespace); } // if multipart then ignore it since no outlook message body is ever // valid multipart! if (StringBeginsWith(str, NS_LITERAL_STRING("multipart/"), nsCaseInsensitiveStringComparator())) str.Truncate(); }
static void ParseAlignAttribute(nsString& aValue, eAlign& aAlign, int32_t& aRowIndex) { // by default, the table is centered about the axis aRowIndex = 0; aAlign = eAlign_axis; int32_t len = 0; // we only have to remove the leading spaces because // ToInteger ignores the whitespaces around the number aValue.CompressWhitespace(true, false); if (0 == aValue.Find("top")) { len = 3; // 3 is the length of 'top' aAlign = eAlign_top; } else if (0 == aValue.Find("bottom")) { len = 6; // 6 is the length of 'bottom' aAlign = eAlign_bottom; } else if (0 == aValue.Find("center")) { len = 6; // 6 is the length of 'center' aAlign = eAlign_center; } else if (0 == aValue.Find("baseline")) { len = 8; // 8 is the length of 'baseline' aAlign = eAlign_baseline; } else if (0 == aValue.Find("axis")) { len = 4; // 4 is the length of 'axis' aAlign = eAlign_axis; } if (len) { nsresult error; aValue.Cut(0, len); // aValue is not a const here aRowIndex = aValue.ToInteger(&error); if (NS_FAILED(error)) aRowIndex = 0; } }
// Strip the windows host process bootstrap executable rundll32.exe // from a handler's command string if it exists. static void StripRundll32(nsString& aCommandString) { // Example rundll formats: // C:\Windows\System32\rundll32.exe "path to dll" // rundll32.exe "path to dll" // C:\Windows\System32\rundll32.exe "path to dll", var var // rundll32.exe "path to dll", var var NS_NAMED_LITERAL_STRING(rundllSegment, "rundll32.exe "); NS_NAMED_LITERAL_STRING(rundllSegmentShort, "rundll32 "); // case insensitive int32_t strLen = rundllSegment.Length(); int32_t index = aCommandString.Find(rundllSegment, true); if (index == kNotFound) { strLen = rundllSegmentShort.Length(); index = aCommandString.Find(rundllSegmentShort, true); } if (index != kNotFound) { uint32_t rundllSegmentLength = index + strLen; aCommandString.Cut(0, rundllSegmentLength); } }
// static void nsTextEditRules::HandleNewLines(nsString &aString, PRInt32 aNewlineHandling) { if (aNewlineHandling < 0) { PRInt32 caretStyle; nsPlaintextEditor::GetDefaultEditorPrefs(aNewlineHandling, caretStyle); } switch(aNewlineHandling) { case nsIPlaintextEditor::eNewlinesReplaceWithSpaces: // Strip trailing newlines first so we don't wind up with trailing spaces aString.Trim(CRLF, false, true); aString.ReplaceChar(CRLF, ' '); break; case nsIPlaintextEditor::eNewlinesStrip: aString.StripChars(CRLF); break; case nsIPlaintextEditor::eNewlinesPasteToFirst: default: { PRInt32 firstCRLF = aString.FindCharInSet(CRLF); // we get first *non-empty* line. PRInt32 offset = 0; while (firstCRLF == offset) { offset++; firstCRLF = aString.FindCharInSet(CRLF, offset); } if (firstCRLF > 0) aString.Truncate(firstCRLF); if (offset > 0) aString.Cut(0, offset); } break; case nsIPlaintextEditor::eNewlinesReplaceWithCommas: aString.Trim(CRLF, true, true); aString.ReplaceChar(CRLF, ','); break; case nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace: { // find each newline, and strip all the whitespace before // and after it PRInt32 firstCRLF = aString.FindCharInSet(CRLF); while (firstCRLF >= 0) { PRUint32 wsBegin = firstCRLF, wsEnd = firstCRLF + 1; // look backwards for the first non-whitespace char while (wsBegin > 0 && NS_IS_SPACE(aString[wsBegin - 1])) --wsBegin; while (wsEnd < aString.Length() && NS_IS_SPACE(aString[wsEnd])) ++wsEnd; // now cut this range out of the string aString.Cut(wsBegin, wsEnd - wsBegin); // look for another CR or LF firstCRLF = aString.FindCharInSet(CRLF); } } break; case nsIPlaintextEditor::eNewlinesPasteIntact: // even if we're pasting newlines, don't paste leading/trailing ones aString.Trim(CRLF, true, true); break; } }
// static void nsTextEditRules::HandleNewLines(nsString &aString, int32_t aNewlineHandling) { if (aNewlineHandling < 0) { int32_t caretStyle; nsPlaintextEditor::GetDefaultEditorPrefs(aNewlineHandling, caretStyle); } switch(aNewlineHandling) { case nsIPlaintextEditor::eNewlinesReplaceWithSpaces: // Strip trailing newlines first so we don't wind up with trailing spaces aString.Trim(CRLF, false, true); aString.ReplaceChar(CRLF, ' '); break; case nsIPlaintextEditor::eNewlinesStrip: aString.StripChars(CRLF); break; case nsIPlaintextEditor::eNewlinesPasteToFirst: default: { int32_t firstCRLF = aString.FindCharInSet(CRLF); // we get first *non-empty* line. int32_t offset = 0; while (firstCRLF == offset) { offset++; firstCRLF = aString.FindCharInSet(CRLF, offset); } if (firstCRLF > 0) aString.Truncate(firstCRLF); if (offset > 0) aString.Cut(0, offset); } break; case nsIPlaintextEditor::eNewlinesReplaceWithCommas: aString.Trim(CRLF, true, true); aString.ReplaceChar(CRLF, ','); break; case nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace: { nsString result; uint32_t offset = 0; while (offset < aString.Length()) { int32_t nextCRLF = aString.FindCharInSet(CRLF, offset); if (nextCRLF < 0) { result.Append(nsDependentSubstring(aString, offset)); break; } uint32_t wsBegin = nextCRLF; // look backwards for the first non-whitespace char while (wsBegin > offset && NS_IS_SPACE(aString[wsBegin - 1])) --wsBegin; result.Append(nsDependentSubstring(aString, offset, wsBegin - offset)); offset = nextCRLF + 1; while (offset < aString.Length() && NS_IS_SPACE(aString[offset])) ++offset; } aString = result; } break; case nsIPlaintextEditor::eNewlinesPasteIntact: // even if we're pasting newlines, don't paste leading/trailing ones aString.Trim(CRLF, true, true); break; } }
void mozTXTToHTMLConv::ScanTXT(const PRUnichar * aInString, PRInt32 aInStringLength, PRUint32 whattodo, nsString& aOutString) { bool doURLs = 0 != (whattodo & kURLs); bool doGlyphSubstitution = 0 != (whattodo & kGlyphSubstitution); bool doStructPhrase = 0 != (whattodo & kStructPhrase); PRUint32 structPhrase_strong = 0; // Number of currently open tags PRUint32 structPhrase_underline = 0; PRUint32 structPhrase_italic = 0; PRUint32 structPhrase_code = 0; nsAutoString outputHTML; // moved here for performance increase for(PRUint32 i = 0; PRInt32(i) < aInStringLength;) { if (doGlyphSubstitution) { PRInt32 glyphTextLen; if (GlyphHit(&aInString[i], aInStringLength - i, i == 0, aOutString, glyphTextLen)) { i += glyphTextLen; continue; } } if (doStructPhrase) { const PRUnichar * newOffset = aInString; PRInt32 newLength = aInStringLength; if (i > 0 ) // skip the first element? { newOffset = &aInString[i-1]; newLength = aInStringLength - i + 1; } switch (aInString[i]) // Performance increase { case '*': if (StructPhraseHit(newOffset, newLength, i == 0, NS_LITERAL_STRING("*").get(), 1, "b", "class=\"moz-txt-star\"", aOutString, structPhrase_strong)) { i++; continue; } break; case '/': if (StructPhraseHit(newOffset, newLength, i == 0, NS_LITERAL_STRING("/").get(), 1, "i", "class=\"moz-txt-slash\"", aOutString, structPhrase_italic)) { i++; continue; } break; case '_': if (StructPhraseHit(newOffset, newLength, i == 0, NS_LITERAL_STRING("_").get(), 1, "span" /* <u> is deprecated */, "class=\"moz-txt-underscore\"", aOutString, structPhrase_underline)) { i++; continue; } break; case '|': if (StructPhraseHit(newOffset, newLength, i == 0, NS_LITERAL_STRING("|").get(), 1, "code", "class=\"moz-txt-verticalline\"", aOutString, structPhrase_code)) { i++; continue; } break; } } if (doURLs) { switch (aInString[i]) { case ':': case '@': case '.': if ( (i == 0 || ((i > 0) && aInString[i - 1] != ' ')) && aInString[i +1] != ' ') // Performance increase { PRInt32 replaceBefore; PRInt32 replaceAfter; if (FindURL(aInString, aInStringLength, i, whattodo, outputHTML, replaceBefore, replaceAfter) && structPhrase_strong + structPhrase_italic + structPhrase_underline + structPhrase_code == 0 /* workaround for bug #19445 */ ) { aOutString.Cut(aOutString.Length() - replaceBefore, replaceBefore); aOutString += outputHTML; i += replaceAfter + 1; continue; } } break; } //switch } switch (aInString[i]) { // Special symbols case '<': case '>': case '&': EscapeChar(aInString[i], aOutString, false); i++; break; // Normal characters default: aOutString += aInString[i]; i++; break; } } }
bool nsEudoraEditor::GetEmbeddedImageCID(PRUint32 aCIDHash, const nsAString & aOldRef, nsString &aCID) { bool foundMatch = false; PRInt32 startImageTag = 0; PRInt32 closeImageTag = 0; while ( (startImageTag = m_body.Find("<img", true, closeImageTag)) != kNotFound ) { closeImageTag = m_body.Find(">", false, startImageTag); // We should always find a close tag, bail if we don't if (closeImageTag == kNotFound) break; // Find the source attribute and make sure it's for our image tag PRInt32 startSrcValue = m_body.Find("src", true, startImageTag); if ( (startSrcValue == kNotFound) || (startSrcValue > closeImageTag) ) continue; // Move past the src startSrcValue += 3; // Move past any whitespace while ( isspace(m_body.CharAt(startSrcValue)) ) ++startSrcValue; // We should find an = now if (m_body.CharAt(startSrcValue) != '=') continue; // Move past = ++startSrcValue; // Move past any whitespace while ( isspace(m_body.CharAt(startSrcValue)) ) ++startSrcValue; // Get the quote char and verify that it's valid char quoteChar = static_cast <char> (m_body.CharAt(startSrcValue)); if ( (quoteChar != '"') && (quoteChar != '\'') ) continue; // Move past the quote ++startSrcValue; PRInt32 endSrcValue = m_body.FindChar(quoteChar, startSrcValue); PRInt32 srcLength = endSrcValue - startSrcValue; nsString srcValue; aCID.Assign(Substring(m_body, startSrcValue, srcLength)); if (aCIDHash != 0) { // Verify source value starts with "cid:" if (!StringBeginsWith(aCID, NS_LITERAL_STRING("cid:"), nsCaseInsensitiveStringComparator())) continue; // Remove "cid:" from the start aCID.Cut(0, 4); PRUint32 hashValue = EudoraHashString( NS_LossyConvertUTF16toASCII(aCID).get() ); foundMatch = (hashValue == aCIDHash); } else { foundMatch = aCID.Equals(aOldRef); } } return foundMatch; }
bool nsTextAddress::GetField(const nsAString &aLine, int32_t index, nsString &field, char16_t delim) { bool result = false; int32_t pos = 0; int32_t maxLen = aLine.Length(); char16_t tab = char16_t('\t'); char16_t doubleQuote = char16_t('"'); field.Truncate(); if (delim == tab) tab = 0; while (index && (pos < maxLen)) { while (((aLine[pos] == char16_t(' ')) || (aLine[pos] == tab)) && (pos < maxLen)) { pos++; } if (pos >= maxLen) break; if (aLine[pos] == doubleQuote) { do { pos++; if (((pos + 1) < maxLen) && (aLine[pos] == doubleQuote) && (aLine[pos + 1] == doubleQuote)) { pos += 2; } } while ((pos < maxLen) && (aLine[pos] != doubleQuote)); if (pos < maxLen) pos++; } if (pos >= maxLen) break; while ((pos < maxLen) && (aLine[pos] != delim)) pos++; if (pos >= maxLen) break; index--; pos++; } if (pos >= maxLen) return result; result = true; while ((pos < maxLen) && ((aLine[pos] == ' ') || (aLine[pos] == tab))) pos++; int32_t fLen = 0; int32_t startPos = pos; bool quoted = false; if (aLine[pos] == '"') { startPos++; fLen = -1; do { pos++; fLen++; if (((pos + 1) < maxLen) && (aLine[pos] == doubleQuote) && (aLine[pos + 1] == doubleQuote)) { quoted = true; pos += 2; fLen += 2; } } while ((pos < maxLen) && (aLine[pos] != doubleQuote)); } else { while ((pos < maxLen) && (aLine[pos] != delim)) { pos++; fLen++; } } if (!fLen) { return result; } field.Append(nsDependentSubstring(aLine, startPos, fLen)); field.Trim(kWhitespace); if (quoted) { int32_t offset = field.Find("\"\""); while (offset != -1) { field.Cut(offset, 1); offset = MsgFind(field, "\"\"", false, offset + 1); } } return result; }
// XXX Code copied from nsHTMLContentSink. It should be shared. void nsRDFParserUtils::StripAndConvert(nsString& aResult) { if ( !aResult.IsEmpty() ) { // Strip quotes if present PRUnichar first = aResult.First(); if ((first == '"') || (first == '\'')) { if (aResult.Last() == first) { aResult.Cut(0, 1); PRInt32 pos = aResult.Length() - 1; if (pos >= 0) { aResult.Cut(pos, 1); } } else { // Mismatched quotes - leave them in } } } // Reduce any entities // XXX Note: as coded today, this will only convert well formed // entities. This may not be compatible enough. // XXX there is a table in navigator that translates some numeric entities // should we be doing that? If so then it needs to live in two places (bad) // so we should add a translate numeric entity method from the parser... char cbuf[100]; PRUint32 i = 0; while (i < aResult.Length()) { // If we have the start of an entity (and it's not at the end of // our string) then translate the entity into it's unicode value. if ((aResult.CharAt(i++) == '&') && (i < aResult.Length())) { PRInt32 start = i - 1; PRUnichar e = aResult.CharAt(i); if (e == '#') { // Convert a numeric character reference i++; char* cp = cbuf; char* limit = cp + sizeof(cbuf) - 1; PRBool ok = PR_FALSE; PRUint32 slen = aResult.Length(); while ((i < slen) && (cp < limit)) { PRUnichar f = aResult.CharAt(i); if (f == ';') { i++; ok = PR_TRUE; break; } if ((f >= '0') && (f <= '9')) { *cp++ = char(f); i++; continue; } break; } if (!ok || (cp == cbuf)) { continue; } *cp = '\0'; if (cp - cbuf > 5) { continue; } PRInt32 ch = PRInt32( ::atoi(cbuf) ); if (ch > 65535) { continue; } // Remove entity from string and replace it with the integer // value. aResult.Cut(start, i - start); aResult.Insert(PRUnichar(ch), start); i = start + 1; } else if (((e >= 'A') && (e <= 'Z')) || ((e >= 'a') && (e <= 'z'))) { // Convert a named entity i++; char* cp = cbuf; char* limit = cp + sizeof(cbuf) - 1; *cp++ = char(e); PRBool ok = PR_FALSE; PRUint32 slen = aResult.Length(); while ((i < slen) && (cp < limit)) { PRUnichar f = aResult.CharAt(i); if (f == ';') { i++; ok = PR_TRUE; break; } if (((f >= '0') && (f <= '9')) || ((f >= 'A') && (f <= 'Z')) || ((f >= 'a') && (f <= 'z'))) { *cp++ = char(f); i++; continue; } break; } if (!ok || (cp == cbuf)) { continue; } *cp = '\0'; PRInt32 ch; // XXX Um, here's where we should be converting a // named entity. I removed this to avoid a link-time // dependency on core raptor. ch = EntityToUnicode(cbuf); if (ch < 0) { continue; } // Remove entity from string and replace it with the integer // value. aResult.Cut(start, i - start); aResult.Insert(PRUnichar(ch), start); i = start + 1; } else if (e == '{') { // Convert a script entity // XXX write me! NS_NOTYETIMPLEMENTED("convert a script entity"); } } } }