NS_StringSetDataRange(nsAString& aStr, uint32_t aCutOffset, uint32_t aCutLength, const char16_t* aData, uint32_t aDataLength) { if (aCutOffset == UINT32_MAX) { // append case if (aData) { aStr.Append(aData, aDataLength); } return NS_OK; // XXX report errors } if (aCutLength == UINT32_MAX) { aCutLength = aStr.Length() - aCutOffset; } if (aData) { if (aDataLength == UINT32_MAX) { aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData)); } else { aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength)); } } else { aStr.Cut(aCutOffset, aCutLength); } return NS_OK; // XXX report errors }
NS_StringSetDataRange(nsAString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength, const PRUnichar *aData, PRUint32 aDataLength) { if (aCutOffset == PR_UINT32_MAX) { // append case if (aData) aStr.Append(aData, aDataLength); return NS_OK; // XXX report errors } if (aCutLength == PR_UINT32_MAX) aCutLength = aStr.Length() - aCutOffset; if (aData) { if (aDataLength == PR_UINT32_MAX) aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData)); else aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength)); } else aStr.Cut(aCutOffset, aCutLength); return NS_OK; // XXX report errors }
void ConvertTexttoAsterisks(nsAccessibleWrap* accWrap, nsAString& aString) { // convert each char to "*" when it's "password text" PRUint32 atkRole = atkRoleMap[accWrap->NativeRole()]; if (atkRole == ATK_ROLE_PASSWORD_TEXT) { for (PRUint32 i = 0; i < aString.Length(); i++) aString.Replace(i, 1, NS_LITERAL_STRING("*")); } }
static void __ReplaceChar (nsAString &string, const PRUnichar replace, const PRUnichar with) { PRInt32 i = string.FindChar (replace); while (i >= 0) { string.Replace (i, 1, &with, 1); i = string.FindChar (replace); } }
static void __ReplaceSubstring (nsAString &string, nsAString &replace, nsAString &with) { PRInt32 i = string.Find (replace); while (i >= 0) { string.Replace (i, replace.Length(), with); i = string.Find (replace); } }
static void ConvertTexttoAsterisks(AccessibleWrap* accWrap, nsAString& aString) { // convert each char to "*" when it's "password text" if (accWrap->NativeRole() == roles::PASSWORD_TEXT) { for (uint32_t i = 0; i < aString.Length(); i++) aString.Replace(i, 1, NS_LITERAL_STRING("*")); } }
bool GetIDFromMinidump(nsILocalFile* minidump, nsAString& id) { if (NS_SUCCEEDED(minidump->GetLeafName(id))) { id.Replace(id.Length() - 4, 4, NS_LITERAL_STRING("")); return true; } return false; }
/** * JS property navigator.language, exposed to web content. * Take first value from Accept-Languages (HTTP header), which is * the "content language" freely set by the user in the Pref window. * * Do not use UI language (chosen app locale) here. * See RFC 2616, Section 15.1.4 "Privacy Issues Connected to Accept Headers" * * "en", "en-US" and "i-cherokee" and "" are valid. * Fallback in case of invalid pref should be "" (empty string), to * let site do fallback, e.g. to site's local language. */ NS_IMETHODIMP Navigator::GetLanguage(nsAString& aLanguage) { // E.g. "de-de, en-us,en". const nsAdoptingString& acceptLang = Preferences::GetLocalizedString("intl.accept_languages"); // Take everything before the first "," or ";", without trailing space. nsCharSeparatedTokenizer langTokenizer(acceptLang, ','); const nsSubstring &firstLangPart = langTokenizer.nextToken(); nsCharSeparatedTokenizer qTokenizer(firstLangPart, ';'); aLanguage.Assign(qTokenizer.nextToken()); // Checks and fixups: // replace "_" with "-" to avoid POSIX/Windows "en_US" notation. if (aLanguage.Length() > 2 && aLanguage[2] == PRUnichar('_')) { aLanguage.Replace(2, 1, PRUnichar('-')); // TODO replace all } // Use uppercase for country part, e.g. "en-US", not "en-us", see BCP47 // only uppercase 2-letter country codes, not "zh-Hant", "de-DE-x-goethe". if (aLanguage.Length() <= 2) { return NS_OK; } nsCharSeparatedTokenizer localeTokenizer(aLanguage, '-'); int32_t pos = 0; bool first = true; while (localeTokenizer.hasMoreTokens()) { const nsSubstring& code = localeTokenizer.nextToken(); if (code.Length() == 2 && !first) { nsAutoString upper(code); ToUpperCase(upper); aLanguage.Replace(pos, code.Length(), upper); } pos += code.Length() + 1; // 1 is the separator first = false; } return NS_OK; }
void nsIDNService::normalizeFullStops(nsAString& s) { nsAString::const_iterator start, end; s.BeginReading(start); s.EndReading(end); int32_t index = 0; while (start != end) { switch (*start) { case 0x3002: case 0xFF0E: case 0xFF61: s.Replace(index, 1, NS_LITERAL_STRING(".")); break; default: break; } start++; index++; } }
/** --------------------------------------------------- * Helper function - Returns either the name or sets the length to zero */ static nsresult GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP, nsAString& aPrinterName) { NS_ENSURE_ARG_POINTER(aPS); aPrinterName.Truncate(); if (!aUsePNP) return NS_OK; // Get the Printer Name from the PrintSettings // to use as a prefix for Pref Names char16_t* prtName = nullptr; nsresult rv = aPS->GetPrinterName(&prtName); NS_ENSURE_SUCCESS(rv, rv); aPrinterName = nsDependentString(prtName); // Convert any whitespaces, carriage returns or newlines to _ // The below algorithm is supposedly faster than using iterators NS_NAMED_LITERAL_STRING(replSubstr, "_"); const char* replaceStr = " \n\r"; int32_t x; for (x=0; x < (int32_t)strlen(replaceStr); x++) { char16_t uChar = replaceStr[x]; int32_t i = 0; while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) { aPrinterName.Replace(i, 1, replSubstr); i++; } } return NS_OK; }
static bool GetCurrentWorkingDirectory(nsAString& workingDirectory) { #if !defined(XP_WIN) && !defined(XP_UNIX) //XXX: your platform should really implement this return false; #elif XP_WIN DWORD requiredLength = GetCurrentDirectoryW(0, nullptr); workingDirectory.SetLength(requiredLength); GetCurrentDirectoryW(workingDirectory.Length(), (LPWSTR)workingDirectory.BeginWriting()); // we got a trailing null there workingDirectory.SetLength(requiredLength); workingDirectory.Replace(workingDirectory.Length() - 1, 1, L'\\'); #elif defined(XP_UNIX) nsAutoCString cwd; // 1024 is just a guess at a sane starting value size_t bufsize = 1024; char* result = nullptr; while (result == nullptr) { cwd.SetLength(bufsize); result = getcwd(cwd.BeginWriting(), cwd.Length()); if (!result) { if (errno != ERANGE) return false; // need to make the buffer bigger bufsize *= 2; } } // size back down to the actual string length cwd.SetLength(strlen(result) + 1); cwd.Replace(cwd.Length() - 1, 1, '/'); workingDirectory = NS_ConvertUTF8toUTF16(cwd); #endif return true; }
void nsCSSSelector::AppendToStringWithoutCombinatorsOrNegations (nsAString& aString, nsCSSStyleSheet* aSheet, PRBool aIsNegated) const { nsAutoString temp; PRBool isPseudoElement = IsPseudoElement(); // For non-pseudo-element selectors or for lone pseudo-elements, deal with // namespace prefixes. PRBool wroteNamespace = PR_FALSE; if (!isPseudoElement || !mNext) { // append the namespace prefix if needed nsXMLNameSpaceMap *sheetNS = aSheet ? aSheet->GetNameSpaceMap() : nsnull; // sheetNS is non-null if and only if we had an @namespace rule. If it's // null, that means that the only namespaces we could have are the // wildcard namespace (which can be implicit in this case) and the "none" // namespace, which then needs to be explicitly specified. if (!sheetNS) { NS_ASSERTION(mNameSpace == kNameSpaceID_Unknown || mNameSpace == kNameSpaceID_None, "How did we get this namespace?"); if (mNameSpace == kNameSpaceID_None) { aString.Append(PRUnichar('|')); wroteNamespace = PR_TRUE; } } else if (sheetNS->FindNameSpaceID(nsnull) == mNameSpace) { // We have the default namespace (possibly including the wildcard // namespace). Do nothing. NS_ASSERTION(mNameSpace == kNameSpaceID_Unknown || CanBeNamespaced(aIsNegated), "How did we end up with this namespace?"); } else if (mNameSpace == kNameSpaceID_None) { NS_ASSERTION(CanBeNamespaced(aIsNegated), "How did we end up with this namespace?"); aString.Append(PRUnichar('|')); wroteNamespace = PR_TRUE; } else if (mNameSpace != kNameSpaceID_Unknown) { NS_ASSERTION(CanBeNamespaced(aIsNegated), "How did we end up with this namespace?"); nsIAtom *prefixAtom = sheetNS->FindPrefix(mNameSpace); NS_ASSERTION(prefixAtom, "how'd we get a non-default namespace " "without a prefix?"); nsStyleUtil::AppendEscapedCSSIdent(nsDependentAtomString(prefixAtom), aString); aString.Append(PRUnichar('|')); wroteNamespace = PR_TRUE; } else { // A selector for an element in any namespace, while the default // namespace is something else. :not() is special in that the default // namespace is not implied for non-type selectors, so if this is a // negated non-type selector we don't need to output an explicit wildcard // namespace here, since those default to a wildcard namespace. if (CanBeNamespaced(aIsNegated)) { aString.AppendLiteral("*|"); wroteNamespace = PR_TRUE; } } } if (!mLowercaseTag) { // Universal selector: avoid writing the universal selector when we // can avoid it, especially since we're required to avoid it for the // inside of :not() if (wroteNamespace || (!mIDList && !mClassList && !mPseudoClassList && !mAttrList && (aIsNegated || !mNegations))) { aString.Append(PRUnichar('*')); } } else { // Append the tag name nsAutoString tag; (isPseudoElement ? mLowercaseTag : mCasedTag)->ToString(tag); if (isPseudoElement) { if (!mNext) { // Lone pseudo-element selector -- toss in a wildcard type selector // XXXldb Why? aString.Append(PRUnichar('*')); } if (!nsCSSPseudoElements::IsCSS2PseudoElement(mLowercaseTag)) { aString.Append(PRUnichar(':')); } // This should not be escaped since (a) the pseudo-element string // has a ":" that can't be escaped and (b) all pseudo-elements at // this point are known, and therefore we know they don't need // escaping. aString.Append(tag); } else { nsStyleUtil::AppendEscapedCSSIdent(tag, aString); } } // Append the id, if there is one if (mIDList) { nsAtomList* list = mIDList; while (list != nsnull) { list->mAtom->ToString(temp); aString.Append(PRUnichar('#')); nsStyleUtil::AppendEscapedCSSIdent(temp, aString); list = list->mNext; } } // Append each class in the linked list if (mClassList) { if (isPseudoElement) { #ifdef MOZ_XUL NS_ABORT_IF_FALSE(nsCSSAnonBoxes::IsTreePseudoElement(mLowercaseTag), "must be tree pseudo-element"); aString.Append(PRUnichar('(')); for (nsAtomList* list = mClassList; list; list = list->mNext) { nsStyleUtil::AppendEscapedCSSIdent(nsDependentAtomString(list->mAtom), aString); aString.Append(PRUnichar(',')); } // replace the final comma with a close-paren aString.Replace(aString.Length() - 1, 1, PRUnichar(')')); #else NS_ERROR("Can't happen"); #endif } else { nsAtomList* list = mClassList; while (list != nsnull) { list->mAtom->ToString(temp); aString.Append(PRUnichar('.')); nsStyleUtil::AppendEscapedCSSIdent(temp, aString); list = list->mNext; } } } // Append each attribute selector in the linked list if (mAttrList) { nsAttrSelector* list = mAttrList; while (list != nsnull) { aString.Append(PRUnichar('[')); // Append the namespace prefix if (list->mNameSpace == kNameSpaceID_Unknown) { aString.Append(PRUnichar('*')); aString.Append(PRUnichar('|')); } else if (list->mNameSpace != kNameSpaceID_None) { if (aSheet) { nsXMLNameSpaceMap *sheetNS = aSheet->GetNameSpaceMap(); nsIAtom *prefixAtom = sheetNS->FindPrefix(list->mNameSpace); // Default namespaces don't apply to attribute selectors, so // we must have a useful prefix. NS_ASSERTION(prefixAtom, "How did we end up with a namespace if the prefix " "is unknown?"); nsAutoString prefix; prefixAtom->ToString(prefix); nsStyleUtil::AppendEscapedCSSIdent(prefix, aString); aString.Append(PRUnichar('|')); } } // Append the attribute name list->mCasedAttr->ToString(temp); nsStyleUtil::AppendEscapedCSSIdent(temp, aString); if (list->mFunction != NS_ATTR_FUNC_SET) { // Append the function if (list->mFunction == NS_ATTR_FUNC_INCLUDES) aString.Append(PRUnichar('~')); else if (list->mFunction == NS_ATTR_FUNC_DASHMATCH) aString.Append(PRUnichar('|')); else if (list->mFunction == NS_ATTR_FUNC_BEGINSMATCH) aString.Append(PRUnichar('^')); else if (list->mFunction == NS_ATTR_FUNC_ENDSMATCH) aString.Append(PRUnichar('$')); else if (list->mFunction == NS_ATTR_FUNC_CONTAINSMATCH) aString.Append(PRUnichar('*')); aString.Append(PRUnichar('=')); // Append the value nsStyleUtil::AppendEscapedCSSString(list->mValue, aString); } aString.Append(PRUnichar(']')); list = list->mNext; } } // Append each pseudo-class in the linked list for (nsPseudoClassList* list = mPseudoClassList; list; list = list->mNext) { nsCSSPseudoClasses::PseudoTypeToString(list->mType, temp); // This should not be escaped since (a) the pseudo-class string // has a ":" that can't be escaped and (b) all pseudo-classes at // this point are known, and therefore we know they don't need // escaping. aString.Append(temp); if (list->u.mMemory) { aString.Append(PRUnichar('(')); if (nsCSSPseudoClasses::HasStringArg(list->mType)) { nsStyleUtil::AppendEscapedCSSIdent( nsDependentString(list->u.mString), aString); } else if (nsCSSPseudoClasses::HasNthPairArg(list->mType)) { PRInt32 a = list->u.mNumbers[0], b = list->u.mNumbers[1]; temp.Truncate(); if (a != 0) { if (a == -1) { temp.Append(PRUnichar('-')); } else if (a != 1) { temp.AppendInt(a); } temp.Append(PRUnichar('n')); } if (b != 0 || a == 0) { if (b >= 0 && a != 0) // check a != 0 for whether we printed above temp.Append(PRUnichar('+')); temp.AppendInt(b); } aString.Append(temp); } else { NS_ASSERTION(nsCSSPseudoClasses::HasSelectorListArg(list->mType), "unexpected pseudo-class"); nsString tmp; list->u.mSelectors->ToString(tmp, aSheet); aString.Append(tmp); } aString.Append(PRUnichar(')')); } } }
/** * Windows API includes regional preferences from the user only * if we pass empty locale string or if the locale string matches * the current locale. * * Since Windows API only allows us to retrieve two options - short/long * we map it to our four options as: * * short -> short * medium -> short * long -> long * full -> long * * In order to produce a single date/time format, we use CLDR pattern * for combined date/time string, since Windows API does not provide an * option for this. */ bool OSPreferences::ReadDateTimePattern(DateTimeFormatStyle aDateStyle, DateTimeFormatStyle aTimeStyle, const nsACString& aLocale, nsAString& aRetVal) { WCHAR buffer[LOCALE_NAME_MAX_LENGTH]; LPWSTR localeName = GetWindowsLocaleFor(aLocale, buffer); bool isDate = aDateStyle != DateTimeFormatStyle::None && aDateStyle != DateTimeFormatStyle::Invalid; bool isTime = aTimeStyle != DateTimeFormatStyle::None && aTimeStyle != DateTimeFormatStyle::Invalid; // If both date and time are wanted, we'll initially read them into a // local string, and then insert them into the overall date+time pattern; // but if only one is needed we'll work directly with the return value. // Set 'str' to point to the string we will use to retrieve patterns // from Windows. nsAutoString tmpStr; nsAString* str; if (isDate && isTime) { if (!GetDateTimeConnectorPattern(NS_ConvertUTF16toUTF8(localeName), aRetVal)) { NS_WARNING("failed to get date/time connector"); aRetVal.AssignLiteral(u"{1} {0}"); } str = &tmpStr; } else if (isDate || isTime) { str = &aRetVal; } else { aRetVal.Truncate(0); return true; } if (isDate) { LCTYPE lcType = ToDateLCType(aDateStyle); size_t len = GetLocaleInfoEx(localeName, lcType, nullptr, 0); if (len == 0) { return false; } // We're doing it to ensure the terminator will fit when Windows writes the data // to its output buffer. See bug 1358159 for details. str->SetLength(len); GetLocaleInfoEx(localeName, lcType, (WCHAR*)str->BeginWriting(), len); str->SetLength(len - 1); // -1 because len counts the null terminator // Windows uses "ddd" and "dddd" for abbreviated and full day names respectively, // https://msdn.microsoft.com/en-us/library/windows/desktop/dd317787(v=vs.85).aspx // but in a CLDR/ICU-style pattern these should be "EEE" and "EEEE". // http://userguide.icu-project.org/formatparse/datetime // So we fix that up here. nsAString::const_iterator start, pos, end; start = str->BeginReading(pos); str->EndReading(end); if (FindInReadable(NS_LITERAL_STRING("dddd"), pos, end)) { str->Replace(pos - start, 4, NS_LITERAL_STRING("EEEE")); } else if (FindInReadable(NS_LITERAL_STRING("ddd"), pos, end)) { str->Replace(pos - start, 3, NS_LITERAL_STRING("EEE")); } // Also, Windows uses lowercase "g" or "gg" for era, but ICU wants uppercase "G" // (it would interpret "g" as "modified Julian day"!). So fix that. int32_t index = str->FindChar('g'); if (index >= 0) { str->Replace(index, 1, 'G'); // If it was a double "gg", just drop the second one. index++; if (str->CharAt(index) == 'g') { str->Cut(index, 1); } } // If time was also requested, we need to substitute the date pattern from Windows // into the date+time format that we have in aRetVal. if (isTime) { nsAString::const_iterator start, pos, end; start = aRetVal.BeginReading(pos); aRetVal.EndReading(end); if (FindInReadable(NS_LITERAL_STRING("{1}"), pos, end)) { aRetVal.Replace(pos - start, 3, tmpStr); } } } if (isTime) { LCTYPE lcType = ToTimeLCType(aTimeStyle); size_t len = GetLocaleInfoEx(localeName, lcType, nullptr, 0); if (len == 0) { return false; } // We're doing it to ensure the terminator will fit when Windows writes the data // to its output buffer. See bug 1358159 for details. str->SetLength(len); GetLocaleInfoEx(localeName, lcType, (WCHAR*)str->BeginWriting(), len); str->SetLength(len - 1); // Windows uses "t" or "tt" for a "time marker" (am/pm indicator), // https://msdn.microsoft.com/en-us/library/windows/desktop/dd318148(v=vs.85).aspx // but in a CLDR/ICU-style pattern that should be "a". // http://userguide.icu-project.org/formatparse/datetime // So we fix that up here. int32_t index = str->FindChar('t'); if (index >= 0) { str->Replace(index, 1, 'a'); index++; if (str->CharAt(index) == 't') { str->Cut(index, 1); } } if (isDate) { nsAString::const_iterator start, pos, end; start = aRetVal.BeginReading(pos); aRetVal.EndReading(end); if (FindInReadable(NS_LITERAL_STRING("{0}"), pos, end)) { aRetVal.Replace(pos - start, 3, tmpStr); } } } return true; }