28 #include <nsDataHashtable.h>
29 #include <nsIStringBundle.h>
30 #include <nsIStringEnumerator.h>
31 #include <nsICharsetConverterManager.h>
32 #include <nsIUnicodeDecoder.h>
33 #include <nsServiceManagerUtils.h>
34 #include <sbIStringBundleService.h>
42 const PRUnichar *begin, *end;
43 aString.BeginReading(&begin, &end);
44 for (
const PRUnichar *current = begin + aOffset; current < end; ++current)
46 for (
const char *pattern = aPattern; *pattern; ++pattern)
48 if (NS_UNLIKELY(*current == PRUnichar(*pattern)))
50 return current - begin;
61 PR_snprintf(buf,
sizeof(buf),
"%llu", val);
62 str.Append(NS_ConvertASCIItoUTF16(buf));
69 NS_LossyConvertUTF16toASCII narrow(str);
70 PRInt32 converted = PR_sscanf(narrow.get(),
"%lld", &result);
73 *rv = NS_ERROR_INVALID_ARG;
81 NS_ASSERTION(check.Equals(str),
"Conversion failed");
94 NS_LossyConvertUTF16toASCII narrow(str);
95 PRInt32 converted = PR_sscanf(narrow.get(),
"%llu", &result);
98 *rv = NS_ERROR_INVALID_ARG;
106 NS_ASSERTION(check.Equals(str),
"Conversion failed");
123 PRUint32 len = NS_StringGetMutableData(aString,
PR_UINT32_MAX, &start);
124 PRUnichar *end = start + len;
127 while (from < end && NS_IsAsciiWhitespace(*from))
134 PRUnichar theChar = *from++;
135 if (NS_IsAsciiWhitespace(theChar)) {
137 while (from < end && NS_IsAsciiWhitespace(*from))
144 if (from == end && theChar ==
' ') {
153 while (to > start && to[-1] ==
' ')
161 aString.SetLength(to - start);
169 NS_ENSURE_ARG_POINTER(aLeft);
170 NS_ENSURE_ARG_POINTER(aRight);
171 NS_ENSURE_ARG_POINTER(_retval);
175 nsDataHashtable<nsStringHashKey, PRUint32> leftValues;
176 PRBool success = leftValues.Init();
177 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
180 while (NS_SUCCEEDED(aLeft->HasMore(&hasMore)) && hasMore) {
182 rv = aLeft->GetNext(value);
183 NS_ENSURE_SUCCESS(rv, rv);
186 if (leftValues.Get(value, &count)) {
190 PRBool success = leftValues.Put(value, count);
191 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
194 while (NS_SUCCEEDED(aRight->HasMore(&hasMore)) && hasMore) {
196 rv = aRight->GetNext(value);
197 NS_ENSURE_SUCCESS(rv, rv);
200 if (!leftValues.Get(value, &count)) {
207 PRBool success = leftValues.Put(value, count);
208 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
211 leftValues.Remove(value);
215 if (leftValues.Count() == 0) {
226 const nsAString& aOldChars,
227 const PRUnichar aNewChar)
229 PRUint32 length = aString.Length();
230 for (PRUint32 index = 0; index < length; index++) {
231 PRUnichar currentChar = aString.CharAt(index);
232 PRInt32 oldCharsIndex = aOldChars.FindChar(currentChar);
233 if (oldCharsIndex > -1)
234 aString.Replace(index, 1, aNewChar);
240 const nsACString& aOldChars,
243 PRUint32 length = aOldString.Length();
244 for (PRUint32 index = 0; index < length; index++) {
245 char currentChar = aOldString.CharAt(index);
246 PRInt32 oldCharsIndex = aOldChars.FindChar(currentChar);
247 if (oldCharsIndex > -1)
248 aOldString.Replace(index, 1, aNewChar);
253 const nsAString &aOldString,
254 const nsAString &aNewString)
256 if (aOldString.Length() == 0) {
261 while (i < aString.Length())
263 PRInt32 r = aString.Find(aOldString, i);
267 aString.Replace(r, aOldString.Length(), aNewString);
268 i += r + aNewString.Length();
276 if (aString.IsEmpty()) {
282 const PRInt32 prefix_table[] = {
284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
285 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
292 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
293 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
294 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
295 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
296 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
297 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
298 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
299 3, 3, 3, 3, 3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
302 PRInt32 bytesRemaining = 0;
303 const nsACString::char_type *begin, *end;
304 aString.BeginReading(&begin, &end);
306 for (; begin != end; ++begin) {
307 PRInt32
next = prefix_table[(
unsigned char)(*begin)];
308 if (bytesRemaining) {
323 bytesRemaining =
next;
331 nsCOMPtr<nsICharsetConverterManager> converterManager =
332 do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
333 NS_ENSURE_SUCCESS(rv, PR_FALSE);
335 nsCOMPtr<nsIUnicodeDecoder> decoder;
336 rv = converterManager->GetUnicodeDecoderRaw(
"UTF-8", getter_AddRefs(decoder));
337 NS_ENSURE_SUCCESS(rv, PR_FALSE);
339 PRInt32 dataLen = aString.Length();
341 rv = decoder->GetMaxLength(aString.BeginReading(), dataLen, &size);
342 NS_ENSURE_SUCCESS(rv, PR_FALSE);
344 PRUnichar *wstr =
reinterpret_cast< PRUnichar *
>( NS_Alloc( (size + 1) *
sizeof( PRUnichar ) ) );
345 rv = decoder->Convert(aString.BeginReading(), &dataLen, wstr, &size);
347 NS_ENSURE_SUCCESS(rv, PR_FALSE);
354 const nsAString& aDelimiter,
355 nsTArray<nsString>& aSubStringArray)
358 aSubStringArray.Clear();
362 PRUint32 delimiterLength = aDelimiter.Length();
363 if (delimiterLength == 0) {
364 aSubStringArray.AppendElement(aString);
369 PRInt32 stringLength = aString.Length();
370 PRInt32 currentOffset = 0;
371 PRInt32 delimiterIndex = 0;
375 delimiterIndex = aString.Find(aDelimiter, currentOffset);
376 if (delimiterIndex < 0)
377 delimiterIndex = stringLength;
380 PRUint32 subStringLength = delimiterIndex - currentOffset;
381 if (subStringLength > 0) {
382 nsDependentSubstring subString(aString, currentOffset, subStringLength);
383 aSubStringArray.AppendElement(subString);
385 aSubStringArray.AppendElement(NS_LITERAL_STRING(
""));
389 currentOffset = delimiterIndex + delimiterLength;
390 }
while (delimiterIndex < stringLength);
395 const nsACString& aDelimiter,
396 nsTArray<nsCString>& aSubStringArray)
399 aSubStringArray.Clear();
403 PRUint32 delimiterLength = aDelimiter.Length();
404 if (delimiterLength == 0) {
405 aSubStringArray.AppendElement(aString);
410 PRInt32 stringLength = aString.Length();
411 PRInt32 currentOffset = 0;
412 PRInt32 delimiterIndex = 0;
416 delimiterIndex = aString.Find(aDelimiter, currentOffset);
417 if (delimiterIndex < 0)
418 delimiterIndex = stringLength;
421 PRUint32 subStringLength = delimiterIndex - currentOffset;
422 if (subStringLength > 0) {
423 nsDependentCSubstring subString(aString, currentOffset, subStringLength);
424 aSubStringArray.AppendElement(subString);
426 aSubStringArray.AppendElement(NS_LITERAL_CSTRING(
""));
430 currentOffset = delimiterIndex + delimiterLength;
431 }
while (delimiterIndex < stringLength);
436 PRExplodedTime exploded;
437 PR_ExplodeTime(aTime, PR_GMTParameters, &exploded);
439 PR_FormatTime(buffer,
sizeof(buffer),
"%Y-%m-%dT%H:%M:%S.0Z", &exploded);
440 return NS_ConvertASCIItoUTF16(buffer);
448 NS_ENSURE_ARG_POINTER(aTime);
463 iso8601TimeString = NS_LossyConvertUTF16toASCII(aISO8601TimeString);
464 nsCString_Split(iso8601TimeString, NS_LITERAL_CSTRING(
"T"), components);
465 NS_ENSURE_TRUE(components.Length() == 2, NS_ERROR_INVALID_ARG);
466 nsCAutoString
date = components[0];
467 nsCAutoString time = components[1];
471 NS_ENSURE_TRUE(components.Length() == 3, NS_ERROR_INVALID_ARG);
472 nsCAutoString
year = components[0];
473 nsCAutoString
month = components[1];
474 nsCAutoString
day = components[2];
477 nsCAutoString timezone;
478 if (time[time.Length() - 1] ==
'Z') {
479 timezone.Assign(NS_LITERAL_CSTRING(
" GMT"));
480 time.SetLength(time.Length() - 1);
484 sbAutoSmprintf timeString = PR_smprintf(
"%s-%s-%s %s%s",
492 status = PR_ParseTimeString(timeString, PR_FALSE, aTime);
493 NS_ENSURE_TRUE(status == PR_SUCCESS, NS_ERROR_FAILURE);
514 const nsAString& aKey,
515 const nsAString& aDefault,
516 class nsIStringBundle* aStringBundle)
521 if (!aDefault.IsVoid())
527 nsCOMPtr<nsIStringBundle> stringBundle = aStringBundle;
531 nsCOMPtr<nsIStringBundleService> stringBundleService =
533 NS_ENSURE_SUCCESS(rv, rv);
535 getter_AddRefs(stringBundle));
536 NS_ENSURE_SUCCESS(rv, rv);
540 nsAutoString stringValue;
541 rv = stringBundle->GetStringFromName(aKey.BeginReading(),
542 getter_Copies(stringValue));
543 NS_ENSURE_SUCCESS(rv, rv);
544 aString = stringValue;
551 const nsAString& aKey)
559 const char* aDefault,
560 class nsIStringBundle* aStringBundle)
564 key = NS_ConvertUTF8toUTF16(aKey);
568 nsAutoString defaultString;
570 defaultString = NS_ConvertUTF8toUTF16(aDefault);
594 const nsAString& aKey,
595 const nsTArray<nsString>& aParams,
596 const nsAString& aDefault,
597 class nsIStringBundle* aStringBundle)
602 if (!aDefault.IsVoid())
608 nsCOMPtr<nsIStringBundle> stringBundle = aStringBundle;
612 nsCOMPtr<nsIStringBundleService> stringBundleService =
614 NS_ENSURE_SUCCESS(rv, rv);
616 getter_AddRefs(stringBundle));
617 NS_ENSURE_SUCCESS(rv, rv);
622 PRUint32 paramCount = aParams.Length();
623 const PRUnichar** params =
static_cast<const PRUnichar**
>
624 (NS_Alloc(paramCount *
sizeof(PRUnichar*)));
625 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
627 for (PRUint32
i = 0;
i < paramCount;
i++) {
628 params[
i] = aParams[
i].get();
632 nsAutoString stringValue;
633 rv = stringBundle->FormatStringFromName(aKey.BeginReading(),
636 getter_Copies(stringValue));
637 NS_ENSURE_SUCCESS(rv, rv);
638 aString = stringValue;
nsresult SBGetLocalizedFormattedString(nsAString &aString, const nsAString &aKey, const nsTArray< nsString > &aParams, const nsAString &aDefault, class nsIStringBundle *aStringBundle)
void nsCString_ReplaceChars(nsACString &aOldString, const nsACString &aOldChars, const char aNewChar)
void SB_CompressWhitespace(nsAString &aString, PRBool aLeading, PRBool aTrailing)
nsresult SB_StringEnumeratorEquals(nsIStringEnumerator *aLeft, nsIStringEnumerator *aRight, PRBool *_retval)
PRUint64 nsString_ToUint64(const nsAString &str, nsresult *rv)
void nsString_ReplaceSubstring(nsAString &aString, const nsAString &aOldString, const nsAString &aNewString)
#define SB_STRINGBUNDLESERVICE_CONTRACTID
#define SB_STRING_BUNDLE_URL
void nsString_ReplaceChar(nsAString &aString, const nsAString &aOldChars, const PRUnichar aNewChar)
PRBool IsUTF8(const nsACString &aString)
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
this _dialogInput val(dateText)
nsString SB_FormatISO8601TimeString(PRTime aTime)
void nsCString_Split(const nsACString &aString, const nsACString &aDelimiter, nsTArray< nsCString > &aSubStringArray)
void AppendInt(nsAString &str, PRUint64 val)
nsresult SBGetLocalizedString(nsAString &aString, const nsAString &aKey, const nsAString &aDefault, class nsIStringBundle *aStringBundle)
StringArrayEnumerator prototype hasMore
PRInt64 nsString_ToInt64(const nsAString &str, nsresult *rv)
_getSelectedPageStyle s i
PRInt32 nsString_FindCharInSet(const nsAString &aString, const char *aPattern, PRInt32 aOffset)
PRBool IsLikelyUTF8(const nsACString &aString)
nsresult SB_ParseISO8601TimeString(const nsAString &aISO8601TimeString, PRTime *aTime)