Back to index

lightning-sunbird  0.9+nobinonly
nsStringSupport.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla.
00015  *
00016  * The Initial Developer of the Original Code is IBM Corporation.
00017  * Portions created by IBM Corporation are Copyright (C) 2004
00018  * IBM Corporation. All Rights Reserved.
00019  *
00020  * Contributor(s):
00021  *   Darin Fisher <darin@meer.net>
00022  *   Masayuki Nakano <masayuki@d-toybox.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsStringSupport_h__
00039 #define nsStringSupport_h__
00040 
00041 #ifndef MOZILLA_INTERNAL_API
00042 
00043 #include "nsStringAPI.h"
00044 #include "nsMemory.h"
00045 #include "prprf.h"
00046 #include "plstr.h"
00047 
00048 #ifndef kNotFound
00049 #define kNotFound -1
00050 #endif
00051 
00052 inline void
00053 AppendIntToString(nsCString &str, PRInt32 value)
00054 {
00055   char buf[32];
00056   PR_snprintf(buf, sizeof(buf), "%d", value);
00057   str.Append(buf);
00058 }
00059 
00060 inline PRInt32
00061 FindCharInString(nsCString &str, char c, PRUint32 offset = 0)
00062 {
00063   NS_ASSERTION(offset <= str.Length(), "invalid offset");
00064   const char *data = str.get();
00065   for (const char *p = data + offset; *p; ++p)
00066     if (*p == c)
00067       return p - data;
00068   return kNotFound;
00069 }
00070 
00071 inline PRInt32
00072 FindCharInString(nsString &str, PRUnichar c, PRUint32 offset = 0)
00073 {
00074   NS_ASSERTION(offset <= str.Length(), "invalid offset");
00075   const PRUnichar *data = str.get();
00076   for (const PRUnichar *p = data + offset; *p; ++p)
00077     if (*p == c)
00078       return p - data;
00079   return kNotFound;
00080 }
00081 
00082 inline PRInt32
00083 FindInString(nsCString &str, const char *needle, PRBool ignoreCase = PR_FALSE)
00084 {
00085   const char *data = str.get(), *p;
00086   if (ignoreCase)
00087     p = PL_strcasestr(data, needle);
00088   else
00089     p = PL_strstr(data, needle);
00090   return p ? p - data : kNotFound;
00091 }
00092 
00093 inline void
00094 NS_CopyUnicodeToNative(const nsAString &input, nsACString &output)
00095 {
00096   NS_UTF16ToCString(input, NS_CSTRING_ENCODING_NATIVE_FILESYSTEM, output);
00097 }
00098 
00099 inline void
00100 NS_CopyNativeToUnicode(const nsACString &input, nsAString &output)
00101 {
00102   NS_CStringToUTF16(input, NS_CSTRING_ENCODING_NATIVE_FILESYSTEM, output);
00103 }
00104 
00105 inline void
00106 CopyUTF16toUTF8(const nsAString &input, nsACString &output)
00107 {
00108   NS_UTF16ToCString(input, NS_CSTRING_ENCODING_UTF8, output);
00109 }
00110 
00111 typedef nsCString nsCAutoString;
00112 typedef nsString nsAutoString;
00113 typedef nsCString nsXPIDLCString;
00114 typedef nsString nsXPIDLString;
00115 
00116 #else // MOZILLA_INTERNAL_API
00117 
00118 #include "nsString.h"
00119 #include "nsNativeCharsetUtils.h"
00120 #include "nsReadableUtils.h"
00121 
00122 inline void
00123 AppendIntToString(nsCString &str, PRInt32 value)
00124 {
00125   str.AppendInt(value);
00126 }
00127 
00128 inline PRInt32
00129 FindCharInString(nsCString &str, char c, PRUint32 offset = 0)
00130 {
00131   return str.FindChar(c, offset);
00132 }
00133 
00134 inline PRInt32
00135 FindCharInString(nsString &str, PRUnichar c, PRUint32 offset = 0)
00136 {
00137   return str.FindChar(c, offset);
00138 }
00139 
00140 inline PRInt32
00141 FindInString(nsCString &str, const char *needle, PRBool ignoreCase = PR_FALSE)
00142 {
00143   return str.Find(needle, ignoreCase);
00144 }
00145 
00146 #endif // MOZILLA_INTERNAL_API
00147 
00148 #endif // !nsStringSupport_h__