sbStringUtils.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #ifndef __SBSTRINGUTILS_H__
28 #define __SBSTRINGUTILS_H__
29 
30 #include <nsStringAPI.h>
31 #include <nsTArray.h>
32 #include <prprf.h>
33 #include <sbMemoryUtils.h>
34 
36 
40 class sbAutoString : public nsAutoString
41 {
42 public:
43  sbAutoString(const char* aValue)
44  {
45  AssignLiteral(aValue);
46  }
47 
49  {
50  char valueStr[64];
51 
52  PR_snprintf(valueStr, sizeof(valueStr), "%d", aValue);
53  AssignLiteral(valueStr);
54  }
55 
56  sbAutoString(PRUint32 aValue,
57  PRBool aHex = PR_FALSE,
58  PRBool aHexPrefix = PR_TRUE)
59  {
60  char valueStr[64];
61 
62  if(!aHex) {
63  PR_snprintf(valueStr, sizeof(valueStr), "%lu", long(aValue));
64  }
65  else {
66  if(aHexPrefix) {
67  PR_snprintf(valueStr, sizeof(valueStr), "0x%lx", long(aValue));
68  }
69  else {
70  PR_snprintf(valueStr, sizeof(valueStr), "%lx", long(aValue));
71  }
72  }
73 
74  AssignLiteral(valueStr);
75  }
76 
78  {
79  char valueStr[64];
80 
81  PR_snprintf(valueStr, sizeof(valueStr), "%lld", aValue);
82  AssignLiteral(valueStr);
83  }
84 
85  sbAutoString(PRUint64 aValue)
86  {
87  char valueStr[64];
88 
89  PR_snprintf(valueStr, sizeof(valueStr), "%llu", aValue);
90  AssignLiteral(valueStr);
91  }
92 
94  {
95  char idString[NSID_LENGTH];
96  aValue->ToProvidedString(idString);
97  AssignLiteral(idString);
98  }
99 };
100 
104 class sbCAutoString : public nsCAutoString
105 {
106 public:
107  sbCAutoString(const char* aValue)
108  {
109  AssignLiteral(aValue);
110  }
111 
113  {
114  char valueStr[64];
115 
116  PR_snprintf(valueStr, sizeof(valueStr), "%d", aValue);
117  AssignLiteral(valueStr);
118  }
119 
121  PRBool aHex = PR_FALSE,
122  PRBool aHexPrefix = PR_TRUE)
123  {
124  char valueStr[64];
125 
126  if(!aHex) {
127  PR_snprintf(valueStr, sizeof(valueStr), "%lu", long(aValue));
128  }
129  else {
130  if(aHexPrefix) {
131  PR_snprintf(valueStr, sizeof(valueStr), "0x%lx", long(aValue));
132  }
133  else {
134  PR_snprintf(valueStr, sizeof(valueStr), "%lx", long(aValue));
135  }
136  }
137 
138  AssignLiteral(valueStr);
139  }
140 
142  {
143  char valueStr[64];
144 
145  PR_snprintf(valueStr, sizeof(valueStr), "%lld", aValue);
146  AssignLiteral(valueStr);
147  }
148 
150  {
151  char valueStr[64];
152 
153  PR_snprintf(valueStr, sizeof(valueStr), "%llu", aValue);
154  AssignLiteral(valueStr);
155  }
156 
158  {
159  char idString[NSID_LENGTH];
160  aValue->ToProvidedString(idString);
161  AssignLiteral(idString);
162  }
163 };
164 
170 class SBVoidString : public nsString
171 {
172 public:
174  {
175  SetIsVoid(PR_TRUE);
176  }
177 };
178 
184 class SBVoidCString : public nsCString
185 {
186 public:
188  {
189  SetIsVoid(PR_TRUE);
190  }
191 };
193 PRInt32 nsString_FindCharInSet(const nsAString& aString,
194  const char *aPattern,
195  PRInt32 aOffset = 0);
196 
197 void AppendInt(nsAString& str, PRUint64 val);
198 
199 PRInt64 nsString_ToInt64(const nsAString& str, nsresult* rv = nsnull);
200 
201 PRUint64 nsString_ToUint64(const nsAString& str, nsresult* rv = nsnull);
202 
213 void SB_CompressWhitespace(nsAString& aString,
214  PRBool aLeading = PR_TRUE,
215  PRBool aTrailing = PR_TRUE);
216 
218  nsIStringEnumerator* aRight,
219  PRBool* _retval);
220 
226 void nsString_ReplaceChar(/* inout */ nsAString& aString,
227  const nsAString& aOldChars,
228  const PRUnichar aNewChar);
229 
230 void
231 nsCString_ReplaceChars(nsACString& aOldString,
232  const nsACString& aOldChars,
233  const char aNewChar);
234 
240 void nsString_ReplaceSubstring(/* inout */ nsAString &aString,
241  const nsAString &aOldString,
242  const nsAString &aNewString);
243 
252 PRBool IsLikelyUTF8(const nsACString& aString);
253 
260 PRBool IsUTF8(const nsACString& aString);
261 
271 void nsString_Split(const nsAString& aString,
272  const nsAString& aDelimiter,
273  nsTArray<nsString>& aSubStringArray);
274 
284 void nsCString_Split(const nsACString& aString,
285  const nsACString& aDelimiter,
286  nsTArray<nsCString>& aSubStringArray);
287 
293 nsString SB_FormatISO8601TimeString(PRTime aTime);
294 
302 nsresult SB_ParseISO8601TimeString(const nsAString& aISO8601TimeString,
303  PRTime* aTime);
304 
305 /*
306  * Songbird string bundle URL.
307  */
308 #define SB_STRING_BUNDLE_URL "chrome://songbird/locale/songbird.properties"
309 
323 nsresult SBGetLocalizedString(nsAString& aString,
324  const nsAString& aKey,
325  const nsAString& aDefault,
326  class nsIStringBundle* aStringBundle = nsnull);
327 
328 nsresult SBGetLocalizedString(nsAString& aString,
329  const nsAString& aKey);
330 
331 nsresult SBGetLocalizedString(nsAString& aString,
332  const char* aKey,
333  const char* aDefault = nsnull,
334  class nsIStringBundle* aStringBundle = nsnull);
335 
336 nsresult SBGetLocalizedFormattedString(nsAString& aString,
337  const nsAString& aKey,
338  const nsTArray<nsString>& aParams,
339  const nsAString& aDefault,
340  class nsIStringBundle* aStringBundle);
341 
347 class SBLocalizedString : public nsString
348 {
349 public:
350  SBLocalizedString(const nsAString& aKey,
351  const nsAString& aDefault,
352  class nsIStringBundle* aStringBundle = nsnull)
353  {
354  nsString stringValue;
355  SBGetLocalizedString(stringValue, aKey, aDefault, aStringBundle);
356  Assign(stringValue);
357  }
358 
359  SBLocalizedString(const nsAString& aKey)
360  {
361  nsString stringValue;
362  SBGetLocalizedString(stringValue, aKey);
363  Assign(stringValue);
364  }
365 
366  SBLocalizedString(const char* aKey,
367  const char* aDefault = nsnull,
368  class nsIStringBundle* aStringBundle = nsnull)
369  {
370  nsString stringValue;
371  SBGetLocalizedString(stringValue, aKey, aDefault, aStringBundle);
372  Assign(stringValue);
373  }
374 
375  SBLocalizedString(const char* aKey,
376  const nsTArray<nsString>& aParams,
377  const char* aDefault = nsnull,
378  class nsIStringBundle* aStringBundle = nsnull)
379  {
380  // Get the key and default arguments.
381  nsString key;
382  key.AssignLiteral(aKey);
383  nsString _default;
384  if (aDefault)
385  _default.AssignLiteral(aDefault);
386  else
387  _default.SetIsVoid(PR_TRUE);
388 
389  // Set string value.
390  nsString stringValue;
391  SBGetLocalizedFormattedString(stringValue,
392  key,
393  aParams,
394  _default,
395  aStringBundle);
396  Assign(stringValue);
397  }
398 
399  SBLocalizedString(const nsAString& aKey,
400  const nsTArray<nsString>& aParams,
401  const nsAString& aDefault,
402  class nsIStringBundle* aStringBundle = nsnull)
403  {
404  nsString stringValue;
405  SBGetLocalizedFormattedString(stringValue,
406  aKey,
407  aParams,
408  aDefault,
409  aStringBundle);
410  Assign(stringValue);
411  }
412 };
413 
414 template <class T>
415 inline
416 T const & sbAppendStringArrayDefaultExtractor(T const & aArrayItem)
417 {
418  return aArrayItem;
419 }
420 
421 template <class S, class Sep, class T, class E>
422 inline
423 S sbAppendStringArray(S & aTarget,
424  Sep const & aSeparator,
425  T const & aStringArray,
426  E aExtractor)
427 {
428  // Save off the lengths
429  PRUint32 const separatorLength = aSeparator.Length();
430  PRUint32 const stringLength = aTarget.Length();
431  PRUint32 const length = aStringArray.Length();
432 
433  // Calc the final length from the original string + length of all the
434  // separators
435  PRUint32 finalLength = stringLength + (separatorLength * (aStringArray.Length() - 1));
436  for (PRUint32 index = 0; index < length; ++index) {
437  finalLength += aStringArray[index].Length();
438  }
439  typename S::char_type const * const separator = aSeparator.BeginReading();
440  // Set the writePosition to the end of the string
441  typename S::char_type * writePosition;
442  typename S::char_type * endPosition;
443  aTarget.BeginWriting(&writePosition, &endPosition, finalLength);
444  if (!writePosition)
445  return aTarget;
446  writePosition += stringLength;
447  // Now append
448  PRBool const isSeparatorEmpty = aSeparator.IsEmpty();
449  for (PRUint32 index = 0; index < length; ++index) {
450  if (index != 0 && !isSeparatorEmpty) {
451  memcpy(writePosition, separator, separatorLength * sizeof(typename S::char_type));
452  writePosition += separatorLength;
453  }
454  S const & value = aExtractor(aStringArray[index]);
455  memcpy(writePosition,
456  value.BeginReading(),
457  value.Length() * sizeof(typename S::char_type));
458  writePosition += value.Length();
459  }
460  return aTarget;
461 }
462 
463 template <class S, class Sep, class T>
464 inline
465 S sbAppendStringArray(S & aTarget,
466  Sep const & aSeparator,
467  T const & aStringArray)
468 {
469  return sbAppendStringArray(aTarget, aSeparator, aStringArray, sbAppendStringArrayDefaultExtractor<S>);
470 }
471 
472 
486 template <class StringType, class EnumeratorType>
487 inline nsresult
488 sbAppendStringEnumerator(StringType& aStringArray,
489  EnumeratorType* aEnumerator)
490 {
491  NS_ENSURE_ARG_POINTER(aEnumerator);
492 
493  // Append all elements from the enumerator.
494  nsresult rv;
495  while (1) {
496  // Check if the enumerator has any more elements.
497  PRBool hasMore;
498  rv = aEnumerator->HasMore(&hasMore);
499  NS_ENSURE_SUCCESS(rv, rv);
500  if (!hasMore)
501  break;
502 
503  // Append the enumerator element to the array.
504  typename StringType::elem_type stringElement;
505  rv = aEnumerator->GetNext(stringElement);
506  NS_ENSURE_SUCCESS(rv, rv);
507  NS_ENSURE_TRUE(aStringArray.AppendElement(stringElement),
508  NS_ERROR_OUT_OF_MEMORY);
509  }
510 
511  return NS_OK;
512 }
513 
514 //
515 // Auto-disposal class wrappers.
516 //
517 // sbAutoSmprintf Wrapper to auto-free strings created by
518 // smprintf.
519 //
520 
521 SB_AUTO_NULL_CLASS(sbAutoSmprintf, char*, PR_smprintf_free(mValue));
522 
527 template <class T>
528 T sbEscapeXML(T const & aSrc)
529 {
530  T result;
531  typename T::char_type const * src;
532  typename T::char_type const * srcEnd;
533  aSrc.BeginReading(&src, &srcEnd);
534 
535  typename T::char_type * destStart;
536  typename T::char_type * destEnd;
537  result.BeginWriting(&destStart, &destEnd, (aSrc.Length() * 6 + 1));
538  typename T::char_type * dest = destStart;
539 
540  while (src != srcEnd) {
541  const char c = *src++;
542  // Escape the character if needed
543  switch (c) {
544  case '<':
545  *dest++ = '&';
546  *dest++ = 'l';
547  *dest++ = 't';
548  *dest++ = ';';
549  break;
550  case '>':
551  *dest++ = '&';
552  *dest++ = 'g';
553  *dest++ = 't';
554  *dest++ = ';';
555  break;
556  case '&':
557  *dest++ = '&';
558  *dest++ = 'a';
559  *dest++ = 'm';
560  *dest++ = 'p';
561  *dest++ = ';';
562  break;
563  case '"':
564  *dest++ = '&';
565  *dest++ = 'q';
566  *dest++ = 'u';
567  *dest++ = 'o';
568  *dest++ = 't';
569  *dest++ = ';';
570  break;
571  case '\'':
572  *dest++ = '&';
573  *dest++ = '#';
574  *dest++ = '3';
575  *dest++ = '9';
576  *dest++ = ';';
577  break;
578  default:
579  *dest++ = c;
580  break;
581  }
582  }
583 
584  result.SetLength(dest - destStart);
585  return result;
586 }
587 
588 #endif /* __SBSTRINGUTILS_H__ */
sbAutoString(nsID *aValue)
Definition: sbStringUtils.h:93
sbCAutoString(nsID *aValue)
SBLocalizedString(const nsAString &aKey)
void SB_CompressWhitespace(nsAString &aString, PRBool aLeading=PR_TRUE, PRBool aTrailing=PR_TRUE)
SBLocalizedString(const char *aKey, const char *aDefault=nsnull, class nsIStringBundle *aStringBundle=nsnull)
sbAutoString(int aValue)
Definition: sbStringUtils.h:48
void AppendInt(nsAString &str, PRUint64 val)
return NS_OK
S sbAppendStringArray(S &aTarget, Sep const &aSeparator, T const &aStringArray, E aExtractor)
sbCAutoString(int aValue)
onPageChanged aValue
Definition: FeedWriter.js:1395
sbAutoString(PRUint64 aValue)
Definition: sbStringUtils.h:85
SBLocalizedString(const nsAString &aKey, const nsTArray< nsString > &aParams, const nsAString &aDefault, class nsIStringBundle *aStringBundle=nsnull)
void nsString_ReplaceChar(nsAString &aString, const nsAString &aOldChars, const PRUnichar aNewChar)
SBLocalizedString(const char *aKey, const nsTArray< nsString > &aParams, const char *aDefault=nsnull, class nsIStringBundle *aStringBundle=nsnull)
PRInt32 nsString_FindCharInSet(const nsAString &aString, const char *aPattern, PRInt32 aOffset=0)
PRBool IsUTF8(const nsACString &aString)
nsString SB_FormatISO8601TimeString(PRTime aTime)
void nsCString_Split(const nsACString &aString, const nsACString &aDelimiter, nsTArray< nsCString > &aSubStringArray)
function E(H, I)
restoreDimensions aLeft
void nsString_ReplaceSubstring(nsAString &aString, const nsAString &aOldString, const nsAString &aNewString)
sbAutoString(const char *aValue)
Definition: sbStringUtils.h:43
nsresult SBGetLocalizedString(nsAString &aString, const nsAString &aKey, const nsAString &aDefault, class nsIStringBundle *aStringBundle=nsnull)
SBLocalizedString(const nsAString &aKey, const nsAString &aDefault, class nsIStringBundle *aStringBundle=nsnull)
void nsCString_ReplaceChars(nsACString &aOldString, const nsACString &aOldChars, const char aNewChar)
SB_AUTO_NULL_CLASS(sbAutoSmprintf, char *, PR_smprintf_free(mValue))
sbCAutoString(PRUint64 aValue)
this _dialogInput val(dateText)
nsresult sbAppendStringEnumerator(StringType &aStringArray, EnumeratorType *aEnumerator)
PRUint64 nsString_ToUint64(const nsAString &str, nsresult *rv=nsnull)
StringArrayEnumerator prototype hasMore
void nsString_Split(const nsAString &aString, const nsAString &aDelimiter, nsTArray< nsString > &aSubStringArray)
countRef value
Definition: FeedWriter.js:1423
sbAutoString(PRInt64 aValue)
Definition: sbStringUtils.h:77
nsresult SB_ParseISO8601TimeString(const nsAString &aISO8601TimeString, PRTime *aTime)
T sbEscapeXML(T const &aSrc)
sbAutoString(PRUint32 aValue, PRBool aHex=PR_FALSE, PRBool aHexPrefix=PR_TRUE)
Definition: sbStringUtils.h:56
sbCAutoString(PRUint32 aValue, PRBool aHex=PR_FALSE, PRBool aHexPrefix=PR_TRUE)
sbCAutoString(const char *aValue)
T const & sbAppendStringArrayDefaultExtractor(T const &aArrayItem)
nsresult SB_StringEnumeratorEquals(nsIStringEnumerator *aLeft, nsIStringEnumerator *aRight, PRBool *_retval)
PRInt64 nsString_ToInt64(const nsAString &str, nsresult *rv=nsnull)
nsresult SBGetLocalizedFormattedString(nsAString &aString, const nsAString &aKey, const nsTArray< nsString > &aParams, const nsAString &aDefault, class nsIStringBundle *aStringBundle)
sbCAutoString(PRInt64 aValue)
PRBool IsLikelyUTF8(const nsACString &aString)