Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim:set ts=2 sw=2 et cindent: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsXPCOMStrings_h__ |
michael@0 | 7 | #define nsXPCOMStrings_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include <string.h> |
michael@0 | 10 | #include "nscore.h" |
michael@0 | 11 | #include <limits> |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * nsXPCOMStrings.h |
michael@0 | 15 | * |
michael@0 | 16 | * This file describes a minimal API for working with XPCOM's abstract |
michael@0 | 17 | * string classes. It divorces the consumer from having any run-time |
michael@0 | 18 | * dependency on the implementation details of the abstract string types. |
michael@0 | 19 | */ |
michael@0 | 20 | |
michael@0 | 21 | #include "nscore.h" |
michael@0 | 22 | |
michael@0 | 23 | /* The base string types */ |
michael@0 | 24 | class nsAString; |
michael@0 | 25 | class nsACString; |
michael@0 | 26 | |
michael@0 | 27 | /* ------------------------------------------------------------------------- */ |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * nsStringContainer |
michael@0 | 31 | * |
michael@0 | 32 | * This is an opaque data type that is large enough to hold the canonical |
michael@0 | 33 | * implementation of nsAString. The binary structure of this class is an |
michael@0 | 34 | * implementation detail. |
michael@0 | 35 | * |
michael@0 | 36 | * The string data stored in a string container is always single fragment |
michael@0 | 37 | * and may be null-terminated depending on how it is initialized. |
michael@0 | 38 | * |
michael@0 | 39 | * Typically, string containers are allocated on the stack for temporary |
michael@0 | 40 | * use. However, they can also be malloc'd if necessary. In either case, |
michael@0 | 41 | * a string container is not useful until it has been initialized with a |
michael@0 | 42 | * call to NS_StringContainerInit. The following example shows how to use |
michael@0 | 43 | * a string container to call a function that takes a |nsAString &| out-param. |
michael@0 | 44 | * |
michael@0 | 45 | * NS_METHOD GetBlah(nsAString &aBlah); |
michael@0 | 46 | * |
michael@0 | 47 | * nsresult MyCode() |
michael@0 | 48 | * { |
michael@0 | 49 | * nsresult rv; |
michael@0 | 50 | * |
michael@0 | 51 | * nsStringContainer sc; |
michael@0 | 52 | * rv = NS_StringContainerInit(sc); |
michael@0 | 53 | * if (NS_FAILED(rv)) |
michael@0 | 54 | * return rv; |
michael@0 | 55 | * |
michael@0 | 56 | * rv = GetBlah(sc); |
michael@0 | 57 | * if (NS_SUCCEEDED(rv)) |
michael@0 | 58 | * { |
michael@0 | 59 | * const char16_t *data; |
michael@0 | 60 | * NS_StringGetData(sc, &data); |
michael@0 | 61 | * // |
michael@0 | 62 | * // |data| now points to the result of the GetBlah function |
michael@0 | 63 | * // |
michael@0 | 64 | * } |
michael@0 | 65 | * |
michael@0 | 66 | * NS_StringContainerFinish(sc); |
michael@0 | 67 | * return rv; |
michael@0 | 68 | * } |
michael@0 | 69 | * |
michael@0 | 70 | * The following example show how to use a string container to pass a string |
michael@0 | 71 | * parameter to a function taking a |const nsAString &| in-param. |
michael@0 | 72 | * |
michael@0 | 73 | * NS_METHOD SetBlah(const nsAString &aBlah); |
michael@0 | 74 | * |
michael@0 | 75 | * nsresult MyCode() |
michael@0 | 76 | * { |
michael@0 | 77 | * nsresult rv; |
michael@0 | 78 | * |
michael@0 | 79 | * nsStringContainer sc; |
michael@0 | 80 | * rv = NS_StringContainerInit(sc); |
michael@0 | 81 | * if (NS_FAILED(rv)) |
michael@0 | 82 | * return rv; |
michael@0 | 83 | * |
michael@0 | 84 | * const char16_t kData[] = {'x','y','z','\0'}; |
michael@0 | 85 | * rv = NS_StringSetData(sc, kData, sizeof(kData)/2 - 1); |
michael@0 | 86 | * if (NS_SUCCEEDED(rv)) |
michael@0 | 87 | * rv = SetBlah(sc); |
michael@0 | 88 | * |
michael@0 | 89 | * NS_StringContainerFinish(sc); |
michael@0 | 90 | * return rv; |
michael@0 | 91 | * } |
michael@0 | 92 | */ |
michael@0 | 93 | class nsStringContainer; |
michael@0 | 94 | |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * This struct is never used directly. It is designed to have the same |
michael@0 | 98 | * size as nsString. It can be stack and heap allocated and the internal |
michael@0 | 99 | * functions cast it to nsString. |
michael@0 | 100 | * While this practice is a strict aliasing violation, it doesn't seem to |
michael@0 | 101 | * cause problems since the the struct is only accessed via the casts to |
michael@0 | 102 | * nsString. |
michael@0 | 103 | * We use protected instead of private to avoid compiler warnings about |
michael@0 | 104 | * the members being unused. |
michael@0 | 105 | */ |
michael@0 | 106 | struct nsStringContainer_base |
michael@0 | 107 | { |
michael@0 | 108 | protected: |
michael@0 | 109 | void *d1; |
michael@0 | 110 | uint32_t d2; |
michael@0 | 111 | uint32_t d3; |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | /** |
michael@0 | 115 | * Flags that may be OR'd together to pass to NS_StringContainerInit2: |
michael@0 | 116 | */ |
michael@0 | 117 | enum { |
michael@0 | 118 | /* Data passed into NS_StringContainerInit2 is not copied; instead, the |
michael@0 | 119 | * string references the passed in data pointer directly. The caller must |
michael@0 | 120 | * ensure that the data is valid for the lifetime of the string container. |
michael@0 | 121 | * This flag should not be combined with NS_STRING_CONTAINER_INIT_ADOPT. */ |
michael@0 | 122 | NS_STRING_CONTAINER_INIT_DEPEND = (1 << 1), |
michael@0 | 123 | |
michael@0 | 124 | /* Data passed into NS_StringContainerInit2 is not copied; instead, the |
michael@0 | 125 | * string takes ownership over the data pointer. The caller must have |
michael@0 | 126 | * allocated the data array using the XPCOM memory allocator (nsMemory). |
michael@0 | 127 | * This flag should not be combined with NS_STRING_CONTAINER_INIT_DEPEND. */ |
michael@0 | 128 | NS_STRING_CONTAINER_INIT_ADOPT = (1 << 2), |
michael@0 | 129 | |
michael@0 | 130 | /* Data passed into NS_StringContainerInit2 is a substring that is not |
michael@0 | 131 | * null-terminated. */ |
michael@0 | 132 | NS_STRING_CONTAINER_INIT_SUBSTRING = (1 << 3) |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | /** |
michael@0 | 136 | * NS_StringContainerInit |
michael@0 | 137 | * |
michael@0 | 138 | * @param aContainer string container reference |
michael@0 | 139 | * @return NS_OK if string container successfully initialized |
michael@0 | 140 | * |
michael@0 | 141 | * This function may allocate additional memory for aContainer. When |
michael@0 | 142 | * aContainer is no longer needed, NS_StringContainerFinish should be called. |
michael@0 | 143 | */ |
michael@0 | 144 | XPCOM_API(nsresult) |
michael@0 | 145 | NS_StringContainerInit(nsStringContainer &aContainer); |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * NS_StringContainerInit2 |
michael@0 | 149 | * |
michael@0 | 150 | * @param aContainer string container reference |
michael@0 | 151 | * @param aData character buffer (may be null) |
michael@0 | 152 | * @param aDataLength number of characters stored at aData (may pass |
michael@0 | 153 | * UINT32_MAX if aData is null-terminated) |
michael@0 | 154 | * @param aFlags flags affecting how the string container is |
michael@0 | 155 | * initialized. this parameter is ignored when aData |
michael@0 | 156 | * is null. otherwise, if this parameter is 0, then |
michael@0 | 157 | * aData is copied into the string. |
michael@0 | 158 | * |
michael@0 | 159 | * This function resembles NS_StringContainerInit but provides further |
michael@0 | 160 | * options that permit more efficient memory usage. When aContainer is |
michael@0 | 161 | * no longer needed, NS_StringContainerFinish should be called. |
michael@0 | 162 | * |
michael@0 | 163 | * NOTE: NS_StringContainerInit2(container, nullptr, 0, 0) is equivalent to |
michael@0 | 164 | * NS_StringContainerInit(container). |
michael@0 | 165 | */ |
michael@0 | 166 | XPCOM_API(nsresult) |
michael@0 | 167 | NS_StringContainerInit2 |
michael@0 | 168 | (nsStringContainer &aContainer, const char16_t *aData = nullptr, |
michael@0 | 169 | uint32_t aDataLength = UINT32_MAX, uint32_t aFlags = 0); |
michael@0 | 170 | |
michael@0 | 171 | /** |
michael@0 | 172 | * NS_StringContainerFinish |
michael@0 | 173 | * |
michael@0 | 174 | * @param aContainer string container reference |
michael@0 | 175 | * |
michael@0 | 176 | * This function frees any memory owned by aContainer. |
michael@0 | 177 | */ |
michael@0 | 178 | XPCOM_API(void) |
michael@0 | 179 | NS_StringContainerFinish(nsStringContainer &aContainer); |
michael@0 | 180 | |
michael@0 | 181 | /* ------------------------------------------------------------------------- */ |
michael@0 | 182 | |
michael@0 | 183 | /** |
michael@0 | 184 | * NS_StringGetData |
michael@0 | 185 | * |
michael@0 | 186 | * This function returns a const character pointer to the string's internal |
michael@0 | 187 | * buffer, the length of the string, and a boolean value indicating whether |
michael@0 | 188 | * or not the buffer is null-terminated. |
michael@0 | 189 | * |
michael@0 | 190 | * @param aStr abstract string reference |
michael@0 | 191 | * @param aData out param that will hold the address of aStr's |
michael@0 | 192 | * internal buffer |
michael@0 | 193 | * @param aTerminated if non-null, this out param will be set to indicate |
michael@0 | 194 | * whether or not aStr's internal buffer is null- |
michael@0 | 195 | * terminated |
michael@0 | 196 | * @return length of aStr's internal buffer |
michael@0 | 197 | */ |
michael@0 | 198 | XPCOM_API(uint32_t) |
michael@0 | 199 | NS_StringGetData |
michael@0 | 200 | (const nsAString &aStr, const char16_t **aData, |
michael@0 | 201 | bool *aTerminated = nullptr); |
michael@0 | 202 | |
michael@0 | 203 | /** |
michael@0 | 204 | * NS_StringGetMutableData |
michael@0 | 205 | * |
michael@0 | 206 | * This function provides mutable access to a string's internal buffer. It |
michael@0 | 207 | * returns a pointer to an array of characters that may be modified. The |
michael@0 | 208 | * returned pointer remains valid until the string object is passed to some |
michael@0 | 209 | * other string function. |
michael@0 | 210 | * |
michael@0 | 211 | * Optionally, this function may be used to resize the string's internal |
michael@0 | 212 | * buffer. The aDataLength parameter specifies the requested length of the |
michael@0 | 213 | * string's internal buffer. By passing some value other than UINT32_MAX, |
michael@0 | 214 | * the caller can request that the buffer be resized to the specified number of |
michael@0 | 215 | * characters before returning. The caller is not responsible for writing a |
michael@0 | 216 | * null-terminator. |
michael@0 | 217 | * |
michael@0 | 218 | * @param aStr abstract string reference |
michael@0 | 219 | * @param aDataLength number of characters to resize the string's internal |
michael@0 | 220 | * buffer to or UINT32_MAX if no resizing is needed |
michael@0 | 221 | * @param aData out param that upon return holds the address of aStr's |
michael@0 | 222 | * internal buffer or null if the function failed |
michael@0 | 223 | * @return number of characters or zero if the function failed |
michael@0 | 224 | * |
michael@0 | 225 | * This function does not necessarily null-terminate aStr after resizing its |
michael@0 | 226 | * internal buffer. The behavior depends on the implementation of the abstract |
michael@0 | 227 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data |
michael@0 | 228 | * will be null-terminated by this function. |
michael@0 | 229 | */ |
michael@0 | 230 | XPCOM_API(uint32_t) |
michael@0 | 231 | NS_StringGetMutableData |
michael@0 | 232 | (nsAString &aStr, uint32_t aDataLength, char16_t **aData); |
michael@0 | 233 | |
michael@0 | 234 | /** |
michael@0 | 235 | * NS_StringCloneData |
michael@0 | 236 | * |
michael@0 | 237 | * This function returns a null-terminated copy of the string's |
michael@0 | 238 | * internal buffer. |
michael@0 | 239 | * |
michael@0 | 240 | * @param aStr abstract string reference |
michael@0 | 241 | * @return null-terminated copy of the string's internal buffer |
michael@0 | 242 | * (it must be free'd using using nsMemory::Free) |
michael@0 | 243 | */ |
michael@0 | 244 | XPCOM_API(char16_t *) |
michael@0 | 245 | NS_StringCloneData |
michael@0 | 246 | (const nsAString &aStr); |
michael@0 | 247 | |
michael@0 | 248 | /** |
michael@0 | 249 | * NS_StringSetData |
michael@0 | 250 | * |
michael@0 | 251 | * This function copies aData into aStr. |
michael@0 | 252 | * |
michael@0 | 253 | * @param aStr abstract string reference |
michael@0 | 254 | * @param aData character buffer |
michael@0 | 255 | * @param aDataLength number of characters to copy from source string (pass |
michael@0 | 256 | * UINT32_MAX to copy until end of aData, designated by |
michael@0 | 257 | * a null character) |
michael@0 | 258 | * @return NS_OK if function succeeded |
michael@0 | 259 | * |
michael@0 | 260 | * This function does not necessarily null-terminate aStr after copying data |
michael@0 | 261 | * from aData. The behavior depends on the implementation of the abstract |
michael@0 | 262 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data |
michael@0 | 263 | * will be null-terminated by this function. |
michael@0 | 264 | */ |
michael@0 | 265 | XPCOM_API(nsresult) |
michael@0 | 266 | NS_StringSetData |
michael@0 | 267 | (nsAString &aStr, const char16_t *aData, |
michael@0 | 268 | uint32_t aDataLength = UINT32_MAX); |
michael@0 | 269 | |
michael@0 | 270 | /** |
michael@0 | 271 | * NS_StringSetDataRange |
michael@0 | 272 | * |
michael@0 | 273 | * This function copies aData into a section of aStr. As a result it can be |
michael@0 | 274 | * used to insert new characters into the string. |
michael@0 | 275 | * |
michael@0 | 276 | * @param aStr abstract string reference |
michael@0 | 277 | * @param aCutOffset starting index where the string's existing data |
michael@0 | 278 | * is to be overwritten (pass UINT32_MAX to cause |
michael@0 | 279 | * aData to be appended to the end of aStr, in which |
michael@0 | 280 | * case the value of aCutLength is ignored). |
michael@0 | 281 | * @param aCutLength number of characters to overwrite starting at |
michael@0 | 282 | * aCutOffset (pass UINT32_MAX to overwrite until the |
michael@0 | 283 | * end of aStr). |
michael@0 | 284 | * @param aData character buffer (pass null to cause this function |
michael@0 | 285 | * to simply remove the "cut" range) |
michael@0 | 286 | * @param aDataLength number of characters to copy from source string (pass |
michael@0 | 287 | * UINT32_MAX to copy until end of aData, designated by |
michael@0 | 288 | * a null character) |
michael@0 | 289 | * @return NS_OK if function succeeded |
michael@0 | 290 | * |
michael@0 | 291 | * This function does not necessarily null-terminate aStr after copying data |
michael@0 | 292 | * from aData. The behavior depends on the implementation of the abstract |
michael@0 | 293 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data |
michael@0 | 294 | * will be null-terminated by this function. |
michael@0 | 295 | */ |
michael@0 | 296 | XPCOM_API(nsresult) |
michael@0 | 297 | NS_StringSetDataRange |
michael@0 | 298 | (nsAString &aStr, uint32_t aCutOffset, uint32_t aCutLength, |
michael@0 | 299 | const char16_t *aData, uint32_t aDataLength = UINT32_MAX); |
michael@0 | 300 | |
michael@0 | 301 | /** |
michael@0 | 302 | * NS_StringCopy |
michael@0 | 303 | * |
michael@0 | 304 | * This function makes aDestStr have the same value as aSrcStr. It is |
michael@0 | 305 | * provided as an optimization. |
michael@0 | 306 | * |
michael@0 | 307 | * @param aDestStr abstract string reference to be modified |
michael@0 | 308 | * @param aSrcStr abstract string reference containing source string |
michael@0 | 309 | * @return NS_OK if function succeeded |
michael@0 | 310 | * |
michael@0 | 311 | * This function does not necessarily null-terminate aDestStr after copying |
michael@0 | 312 | * data from aSrcStr. The behavior depends on the implementation of the |
michael@0 | 313 | * abstract string, aDestStr. If aDestStr is a reference to a |
michael@0 | 314 | * nsStringContainer, then its data will be null-terminated by this function. |
michael@0 | 315 | */ |
michael@0 | 316 | XPCOM_API(nsresult) |
michael@0 | 317 | NS_StringCopy |
michael@0 | 318 | (nsAString &aDestStr, const nsAString &aSrcStr); |
michael@0 | 319 | |
michael@0 | 320 | /** |
michael@0 | 321 | * NS_StringAppendData |
michael@0 | 322 | * |
michael@0 | 323 | * This function appends data to the existing value of aStr. |
michael@0 | 324 | * |
michael@0 | 325 | * @param aStr abstract string reference to be modified |
michael@0 | 326 | * @param aData character buffer |
michael@0 | 327 | * @param aDataLength number of characters to append (pass UINT32_MAX to |
michael@0 | 328 | * append until a null-character is encountered) |
michael@0 | 329 | * @return NS_OK if function succeeded |
michael@0 | 330 | * |
michael@0 | 331 | * This function does not necessarily null-terminate aStr upon completion. |
michael@0 | 332 | * The behavior depends on the implementation of the abstract string, aStr. |
michael@0 | 333 | * If aStr is a reference to a nsStringContainer, then its data will be null- |
michael@0 | 334 | * terminated by this function. |
michael@0 | 335 | */ |
michael@0 | 336 | inline NS_HIDDEN_(nsresult) |
michael@0 | 337 | NS_StringAppendData(nsAString &aStr, const char16_t *aData, |
michael@0 | 338 | uint32_t aDataLength = UINT32_MAX) |
michael@0 | 339 | { |
michael@0 | 340 | return NS_StringSetDataRange(aStr, UINT32_MAX, 0, aData, aDataLength); |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | /** |
michael@0 | 344 | * NS_StringInsertData |
michael@0 | 345 | * |
michael@0 | 346 | * This function inserts data into the existing value of aStr at the specified |
michael@0 | 347 | * offset. |
michael@0 | 348 | * |
michael@0 | 349 | * @param aStr abstract string reference to be modified |
michael@0 | 350 | * @param aOffset specifies where in the string to insert aData |
michael@0 | 351 | * @param aData character buffer |
michael@0 | 352 | * @param aDataLength number of characters to append (pass UINT32_MAX to |
michael@0 | 353 | * append until a null-character is encountered) |
michael@0 | 354 | * @return NS_OK if function succeeded |
michael@0 | 355 | * |
michael@0 | 356 | * This function does not necessarily null-terminate aStr upon completion. |
michael@0 | 357 | * The behavior depends on the implementation of the abstract string, aStr. |
michael@0 | 358 | * If aStr is a reference to a nsStringContainer, then its data will be null- |
michael@0 | 359 | * terminated by this function. |
michael@0 | 360 | */ |
michael@0 | 361 | inline NS_HIDDEN_(nsresult) |
michael@0 | 362 | NS_StringInsertData(nsAString &aStr, uint32_t aOffset, const char16_t *aData, |
michael@0 | 363 | uint32_t aDataLength = UINT32_MAX) |
michael@0 | 364 | { |
michael@0 | 365 | return NS_StringSetDataRange(aStr, aOffset, 0, aData, aDataLength); |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | /** |
michael@0 | 369 | * NS_StringCutData |
michael@0 | 370 | * |
michael@0 | 371 | * This function shortens the existing value of aStr, by removing characters |
michael@0 | 372 | * at the specified offset. |
michael@0 | 373 | * |
michael@0 | 374 | * @param aStr abstract string reference to be modified |
michael@0 | 375 | * @param aCutOffset specifies where in the string to insert aData |
michael@0 | 376 | * @param aCutLength number of characters to remove |
michael@0 | 377 | * @return NS_OK if function succeeded |
michael@0 | 378 | */ |
michael@0 | 379 | inline NS_HIDDEN_(nsresult) |
michael@0 | 380 | NS_StringCutData(nsAString &aStr, uint32_t aCutOffset, uint32_t aCutLength) |
michael@0 | 381 | { |
michael@0 | 382 | return NS_StringSetDataRange(aStr, aCutOffset, aCutLength, nullptr, 0); |
michael@0 | 383 | } |
michael@0 | 384 | |
michael@0 | 385 | /** |
michael@0 | 386 | * NS_StringSetIsVoid |
michael@0 | 387 | * |
michael@0 | 388 | * This function marks a string as being a "void string". Any data in the |
michael@0 | 389 | * string will be lost. |
michael@0 | 390 | */ |
michael@0 | 391 | XPCOM_API(void) |
michael@0 | 392 | NS_StringSetIsVoid(nsAString& aStr, const bool aIsVoid); |
michael@0 | 393 | |
michael@0 | 394 | /** |
michael@0 | 395 | * NS_StringGetIsVoid |
michael@0 | 396 | * |
michael@0 | 397 | * This function provides a way to test if a string is a "void string", as |
michael@0 | 398 | * marked by NS_StringSetIsVoid. |
michael@0 | 399 | */ |
michael@0 | 400 | XPCOM_API(bool) |
michael@0 | 401 | NS_StringGetIsVoid(const nsAString& aStr); |
michael@0 | 402 | |
michael@0 | 403 | /* ------------------------------------------------------------------------- */ |
michael@0 | 404 | |
michael@0 | 405 | /** |
michael@0 | 406 | * nsCStringContainer |
michael@0 | 407 | * |
michael@0 | 408 | * This is an opaque data type that is large enough to hold the canonical |
michael@0 | 409 | * implementation of nsACString. The binary structure of this class is an |
michael@0 | 410 | * implementation detail. |
michael@0 | 411 | * |
michael@0 | 412 | * The string data stored in a string container is always single fragment |
michael@0 | 413 | * and may be null-terminated depending on how it is initialized. |
michael@0 | 414 | * |
michael@0 | 415 | * @see nsStringContainer for use cases and further documentation. |
michael@0 | 416 | */ |
michael@0 | 417 | class nsCStringContainer; |
michael@0 | 418 | |
michael@0 | 419 | /** |
michael@0 | 420 | * Flags that may be OR'd together to pass to NS_StringContainerInit2: |
michael@0 | 421 | */ |
michael@0 | 422 | enum { |
michael@0 | 423 | /* Data passed into NS_CStringContainerInit2 is not copied; instead, the |
michael@0 | 424 | * string references the passed in data pointer directly. The caller must |
michael@0 | 425 | * ensure that the data is valid for the lifetime of the string container. |
michael@0 | 426 | * This flag should not be combined with NS_CSTRING_CONTAINER_INIT_ADOPT. */ |
michael@0 | 427 | NS_CSTRING_CONTAINER_INIT_DEPEND = (1 << 1), |
michael@0 | 428 | |
michael@0 | 429 | /* Data passed into NS_CStringContainerInit2 is not copied; instead, the |
michael@0 | 430 | * string takes ownership over the data pointer. The caller must have |
michael@0 | 431 | * allocated the data array using the XPCOM memory allocator (nsMemory). |
michael@0 | 432 | * This flag should not be combined with NS_CSTRING_CONTAINER_INIT_DEPEND. */ |
michael@0 | 433 | NS_CSTRING_CONTAINER_INIT_ADOPT = (1 << 2), |
michael@0 | 434 | |
michael@0 | 435 | /* Data passed into NS_CStringContainerInit2 is a substring that is not |
michael@0 | 436 | * null-terminated. */ |
michael@0 | 437 | NS_CSTRING_CONTAINER_INIT_SUBSTRING = (1 << 3) |
michael@0 | 438 | }; |
michael@0 | 439 | |
michael@0 | 440 | /** |
michael@0 | 441 | * NS_CStringContainerInit |
michael@0 | 442 | * |
michael@0 | 443 | * @param aContainer string container reference |
michael@0 | 444 | * @return NS_OK if string container successfully initialized |
michael@0 | 445 | * |
michael@0 | 446 | * This function may allocate additional memory for aContainer. When |
michael@0 | 447 | * aContainer is no longer needed, NS_CStringContainerFinish should be called. |
michael@0 | 448 | */ |
michael@0 | 449 | XPCOM_API(nsresult) |
michael@0 | 450 | NS_CStringContainerInit(nsCStringContainer &aContainer); |
michael@0 | 451 | |
michael@0 | 452 | /** |
michael@0 | 453 | * NS_CStringContainerInit2 |
michael@0 | 454 | * |
michael@0 | 455 | * @param aContainer string container reference |
michael@0 | 456 | * @param aData character buffer (may be null) |
michael@0 | 457 | * @param aDataLength number of characters stored at aData (may pass |
michael@0 | 458 | * UINT32_MAX if aData is null-terminated) |
michael@0 | 459 | * @param aFlags flags affecting how the string container is |
michael@0 | 460 | * initialized. this parameter is ignored when aData |
michael@0 | 461 | * is null. otherwise, if this parameter is 0, then |
michael@0 | 462 | * aData is copied into the string. |
michael@0 | 463 | * |
michael@0 | 464 | * This function resembles NS_CStringContainerInit but provides further |
michael@0 | 465 | * options that permit more efficient memory usage. When aContainer is |
michael@0 | 466 | * no longer needed, NS_CStringContainerFinish should be called. |
michael@0 | 467 | * |
michael@0 | 468 | * NOTE: NS_CStringContainerInit2(container, nullptr, 0, 0) is equivalent to |
michael@0 | 469 | * NS_CStringContainerInit(container). |
michael@0 | 470 | */ |
michael@0 | 471 | XPCOM_API(nsresult) |
michael@0 | 472 | NS_CStringContainerInit2 |
michael@0 | 473 | (nsCStringContainer &aContainer, const char *aData = nullptr, |
michael@0 | 474 | uint32_t aDataLength = UINT32_MAX, uint32_t aFlags = 0); |
michael@0 | 475 | |
michael@0 | 476 | /** |
michael@0 | 477 | * NS_CStringContainerFinish |
michael@0 | 478 | * |
michael@0 | 479 | * @param aContainer string container reference |
michael@0 | 480 | * |
michael@0 | 481 | * This function frees any memory owned by aContainer. |
michael@0 | 482 | */ |
michael@0 | 483 | XPCOM_API(void) |
michael@0 | 484 | NS_CStringContainerFinish(nsCStringContainer &aContainer); |
michael@0 | 485 | |
michael@0 | 486 | /* ------------------------------------------------------------------------- */ |
michael@0 | 487 | |
michael@0 | 488 | /** |
michael@0 | 489 | * NS_CStringGetData |
michael@0 | 490 | * |
michael@0 | 491 | * This function returns a const character pointer to the string's internal |
michael@0 | 492 | * buffer, the length of the string, and a boolean value indicating whether |
michael@0 | 493 | * or not the buffer is null-terminated. |
michael@0 | 494 | * |
michael@0 | 495 | * @param aStr abstract string reference |
michael@0 | 496 | * @param aData out param that will hold the address of aStr's |
michael@0 | 497 | * internal buffer |
michael@0 | 498 | * @param aTerminated if non-null, this out param will be set to indicate |
michael@0 | 499 | * whether or not aStr's internal buffer is null- |
michael@0 | 500 | * terminated |
michael@0 | 501 | * @return length of aStr's internal buffer |
michael@0 | 502 | */ |
michael@0 | 503 | XPCOM_API(uint32_t) |
michael@0 | 504 | NS_CStringGetData |
michael@0 | 505 | (const nsACString &aStr, const char **aData, |
michael@0 | 506 | bool *aTerminated = nullptr); |
michael@0 | 507 | |
michael@0 | 508 | /** |
michael@0 | 509 | * NS_CStringGetMutableData |
michael@0 | 510 | * |
michael@0 | 511 | * This function provides mutable access to a string's internal buffer. It |
michael@0 | 512 | * returns a pointer to an array of characters that may be modified. The |
michael@0 | 513 | * returned pointer remains valid until the string object is passed to some |
michael@0 | 514 | * other string function. |
michael@0 | 515 | * |
michael@0 | 516 | * Optionally, this function may be used to resize the string's internal |
michael@0 | 517 | * buffer. The aDataLength parameter specifies the requested length of the |
michael@0 | 518 | * string's internal buffer. By passing some value other than UINT32_MAX, |
michael@0 | 519 | * the caller can request that the buffer be resized to the specified number of |
michael@0 | 520 | * characters before returning. The caller is not responsible for writing a |
michael@0 | 521 | * null-terminator. |
michael@0 | 522 | * |
michael@0 | 523 | * @param aStr abstract string reference |
michael@0 | 524 | * @param aDataLength number of characters to resize the string's internal |
michael@0 | 525 | * buffer to or UINT32_MAX if no resizing is needed |
michael@0 | 526 | * @param aData out param that upon return holds the address of aStr's |
michael@0 | 527 | * internal buffer or null if the function failed |
michael@0 | 528 | * @return number of characters or zero if the function failed |
michael@0 | 529 | * |
michael@0 | 530 | * This function does not necessarily null-terminate aStr after resizing its |
michael@0 | 531 | * internal buffer. The behavior depends on the implementation of the abstract |
michael@0 | 532 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data |
michael@0 | 533 | * will be null-terminated by this function. |
michael@0 | 534 | */ |
michael@0 | 535 | XPCOM_API(uint32_t) |
michael@0 | 536 | NS_CStringGetMutableData |
michael@0 | 537 | (nsACString &aStr, uint32_t aDataLength, char **aData); |
michael@0 | 538 | |
michael@0 | 539 | /** |
michael@0 | 540 | * NS_CStringCloneData |
michael@0 | 541 | * |
michael@0 | 542 | * This function returns a null-terminated copy of the string's |
michael@0 | 543 | * internal buffer. |
michael@0 | 544 | * |
michael@0 | 545 | * @param aStr abstract string reference |
michael@0 | 546 | * @return null-terminated copy of the string's internal buffer |
michael@0 | 547 | * (it must be free'd using using nsMemory::Free) |
michael@0 | 548 | */ |
michael@0 | 549 | XPCOM_API(char *) |
michael@0 | 550 | NS_CStringCloneData |
michael@0 | 551 | (const nsACString &aStr); |
michael@0 | 552 | |
michael@0 | 553 | /** |
michael@0 | 554 | * NS_CStringSetData |
michael@0 | 555 | * |
michael@0 | 556 | * This function copies aData into aStr. |
michael@0 | 557 | * |
michael@0 | 558 | * @param aStr abstract string reference |
michael@0 | 559 | * @param aData character buffer |
michael@0 | 560 | * @param aDataLength number of characters to copy from source string (pass |
michael@0 | 561 | * UINT32_MAX to copy until end of aData, designated by |
michael@0 | 562 | * a null character) |
michael@0 | 563 | * @return NS_OK if function succeeded |
michael@0 | 564 | * |
michael@0 | 565 | * This function does not necessarily null-terminate aStr after copying data |
michael@0 | 566 | * from aData. The behavior depends on the implementation of the abstract |
michael@0 | 567 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data |
michael@0 | 568 | * will be null-terminated by this function. |
michael@0 | 569 | */ |
michael@0 | 570 | XPCOM_API(nsresult) |
michael@0 | 571 | NS_CStringSetData |
michael@0 | 572 | (nsACString &aStr, const char *aData, |
michael@0 | 573 | uint32_t aDataLength = UINT32_MAX); |
michael@0 | 574 | |
michael@0 | 575 | /** |
michael@0 | 576 | * NS_CStringSetDataRange |
michael@0 | 577 | * |
michael@0 | 578 | * This function copies aData into a section of aStr. As a result it can be |
michael@0 | 579 | * used to insert new characters into the string. |
michael@0 | 580 | * |
michael@0 | 581 | * @param aStr abstract string reference |
michael@0 | 582 | * @param aCutOffset starting index where the string's existing data |
michael@0 | 583 | * is to be overwritten (pass UINT32_MAX to cause |
michael@0 | 584 | * aData to be appended to the end of aStr, in which |
michael@0 | 585 | * case the value of aCutLength is ignored). |
michael@0 | 586 | * @param aCutLength number of characters to overwrite starting at |
michael@0 | 587 | * aCutOffset (pass UINT32_MAX to overwrite until the |
michael@0 | 588 | * end of aStr). |
michael@0 | 589 | * @param aData character buffer (pass null to cause this function |
michael@0 | 590 | * to simply remove the "cut" range) |
michael@0 | 591 | * @param aDataLength number of characters to copy from source string (pass |
michael@0 | 592 | * UINT32_MAX to copy until end of aData, designated by |
michael@0 | 593 | * a null character) |
michael@0 | 594 | * @return NS_OK if function succeeded |
michael@0 | 595 | * |
michael@0 | 596 | * This function does not necessarily null-terminate aStr after copying data |
michael@0 | 597 | * from aData. The behavior depends on the implementation of the abstract |
michael@0 | 598 | * string, aStr. If aStr is a reference to a nsStringContainer, then its data |
michael@0 | 599 | * will be null-terminated by this function. |
michael@0 | 600 | */ |
michael@0 | 601 | XPCOM_API(nsresult) |
michael@0 | 602 | NS_CStringSetDataRange |
michael@0 | 603 | (nsACString &aStr, uint32_t aCutOffset, uint32_t aCutLength, |
michael@0 | 604 | const char *aData, uint32_t aDataLength = UINT32_MAX); |
michael@0 | 605 | |
michael@0 | 606 | /** |
michael@0 | 607 | * NS_CStringCopy |
michael@0 | 608 | * |
michael@0 | 609 | * This function makes aDestStr have the same value as aSrcStr. It is |
michael@0 | 610 | * provided as an optimization. |
michael@0 | 611 | * |
michael@0 | 612 | * @param aDestStr abstract string reference to be modified |
michael@0 | 613 | * @param aSrcStr abstract string reference containing source string |
michael@0 | 614 | * @return NS_OK if function succeeded |
michael@0 | 615 | * |
michael@0 | 616 | * This function does not necessarily null-terminate aDestStr after copying |
michael@0 | 617 | * data from aSrcStr. The behavior depends on the implementation of the |
michael@0 | 618 | * abstract string, aDestStr. If aDestStr is a reference to a |
michael@0 | 619 | * nsStringContainer, then its data will be null-terminated by this function. |
michael@0 | 620 | */ |
michael@0 | 621 | XPCOM_API(nsresult) |
michael@0 | 622 | NS_CStringCopy |
michael@0 | 623 | (nsACString &aDestStr, const nsACString &aSrcStr); |
michael@0 | 624 | |
michael@0 | 625 | /** |
michael@0 | 626 | * NS_CStringAppendData |
michael@0 | 627 | * |
michael@0 | 628 | * This function appends data to the existing value of aStr. |
michael@0 | 629 | * |
michael@0 | 630 | * @param aStr abstract string reference to be modified |
michael@0 | 631 | * @param aData character buffer |
michael@0 | 632 | * @param aDataLength number of characters to append (pass UINT32_MAX to |
michael@0 | 633 | * append until a null-character is encountered) |
michael@0 | 634 | * @return NS_OK if function succeeded |
michael@0 | 635 | * |
michael@0 | 636 | * This function does not necessarily null-terminate aStr upon completion. |
michael@0 | 637 | * The behavior depends on the implementation of the abstract string, aStr. |
michael@0 | 638 | * If aStr is a reference to a nsStringContainer, then its data will be null- |
michael@0 | 639 | * terminated by this function. |
michael@0 | 640 | */ |
michael@0 | 641 | inline NS_HIDDEN_(nsresult) |
michael@0 | 642 | NS_CStringAppendData(nsACString &aStr, const char *aData, |
michael@0 | 643 | uint32_t aDataLength = UINT32_MAX) |
michael@0 | 644 | { |
michael@0 | 645 | return NS_CStringSetDataRange(aStr, UINT32_MAX, 0, aData, aDataLength); |
michael@0 | 646 | } |
michael@0 | 647 | |
michael@0 | 648 | /** |
michael@0 | 649 | * NS_CStringInsertData |
michael@0 | 650 | * |
michael@0 | 651 | * This function inserts data into the existing value of aStr at the specified |
michael@0 | 652 | * offset. |
michael@0 | 653 | * |
michael@0 | 654 | * @param aStr abstract string reference to be modified |
michael@0 | 655 | * @param aOffset specifies where in the string to insert aData |
michael@0 | 656 | * @param aData character buffer |
michael@0 | 657 | * @param aDataLength number of characters to append (pass UINT32_MAX to |
michael@0 | 658 | * append until a null-character is encountered) |
michael@0 | 659 | * @return NS_OK if function succeeded |
michael@0 | 660 | * |
michael@0 | 661 | * This function does not necessarily null-terminate aStr upon completion. |
michael@0 | 662 | * The behavior depends on the implementation of the abstract string, aStr. |
michael@0 | 663 | * If aStr is a reference to a nsStringContainer, then its data will be null- |
michael@0 | 664 | * terminated by this function. |
michael@0 | 665 | */ |
michael@0 | 666 | inline NS_HIDDEN_(nsresult) |
michael@0 | 667 | NS_CStringInsertData(nsACString &aStr, uint32_t aOffset, const char *aData, |
michael@0 | 668 | uint32_t aDataLength = UINT32_MAX) |
michael@0 | 669 | { |
michael@0 | 670 | return NS_CStringSetDataRange(aStr, aOffset, 0, aData, aDataLength); |
michael@0 | 671 | } |
michael@0 | 672 | |
michael@0 | 673 | /** |
michael@0 | 674 | * NS_CStringCutData |
michael@0 | 675 | * |
michael@0 | 676 | * This function shortens the existing value of aStr, by removing characters |
michael@0 | 677 | * at the specified offset. |
michael@0 | 678 | * |
michael@0 | 679 | * @param aStr abstract string reference to be modified |
michael@0 | 680 | * @param aCutOffset specifies where in the string to insert aData |
michael@0 | 681 | * @param aCutLength number of characters to remove |
michael@0 | 682 | * @return NS_OK if function succeeded |
michael@0 | 683 | */ |
michael@0 | 684 | inline NS_HIDDEN_(nsresult) |
michael@0 | 685 | NS_CStringCutData(nsACString &aStr, uint32_t aCutOffset, uint32_t aCutLength) |
michael@0 | 686 | { |
michael@0 | 687 | return NS_CStringSetDataRange(aStr, aCutOffset, aCutLength, nullptr, 0); |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | /** |
michael@0 | 691 | * NS_CStringSetIsVoid |
michael@0 | 692 | * |
michael@0 | 693 | * This function marks a string as being a "void string". Any data in the |
michael@0 | 694 | * string will be lost. |
michael@0 | 695 | */ |
michael@0 | 696 | XPCOM_API(void) |
michael@0 | 697 | NS_CStringSetIsVoid(nsACString& aStr, const bool aIsVoid); |
michael@0 | 698 | |
michael@0 | 699 | /** |
michael@0 | 700 | * NS_CStringGetIsVoid |
michael@0 | 701 | * |
michael@0 | 702 | * This function provides a way to test if a string is a "void string", as |
michael@0 | 703 | * marked by NS_CStringSetIsVoid. |
michael@0 | 704 | */ |
michael@0 | 705 | XPCOM_API(bool) |
michael@0 | 706 | NS_CStringGetIsVoid(const nsACString& aStr); |
michael@0 | 707 | |
michael@0 | 708 | /* ------------------------------------------------------------------------- */ |
michael@0 | 709 | |
michael@0 | 710 | /** |
michael@0 | 711 | * Encodings that can be used with the following conversion routines. |
michael@0 | 712 | */ |
michael@0 | 713 | enum nsCStringEncoding { |
michael@0 | 714 | /* Conversion between ASCII and UTF-16 assumes that all bytes in the source |
michael@0 | 715 | * string are 7-bit ASCII and can be inflated to UTF-16 by inserting null |
michael@0 | 716 | * bytes. Reverse conversion is done by truncating every other byte. The |
michael@0 | 717 | * conversion may result in loss and/or corruption of information if the |
michael@0 | 718 | * strings do not strictly contain ASCII data. */ |
michael@0 | 719 | NS_CSTRING_ENCODING_ASCII = 0, |
michael@0 | 720 | |
michael@0 | 721 | /* Conversion between UTF-8 and UTF-16 is non-lossy. */ |
michael@0 | 722 | NS_CSTRING_ENCODING_UTF8 = 1, |
michael@0 | 723 | |
michael@0 | 724 | /* Conversion from UTF-16 to the native filesystem charset may result in a |
michael@0 | 725 | * loss of information. No attempt is made to protect against data loss in |
michael@0 | 726 | * this case. The native filesystem charset applies to strings passed to |
michael@0 | 727 | * the "Native" method variants on nsIFile. */ |
michael@0 | 728 | NS_CSTRING_ENCODING_NATIVE_FILESYSTEM = 2 |
michael@0 | 729 | }; |
michael@0 | 730 | |
michael@0 | 731 | /** |
michael@0 | 732 | * NS_CStringToUTF16 |
michael@0 | 733 | * |
michael@0 | 734 | * This function converts the characters in a nsACString to an array of UTF-16 |
michael@0 | 735 | * characters, in the platform endianness. The result is stored in a nsAString |
michael@0 | 736 | * object. |
michael@0 | 737 | * |
michael@0 | 738 | * @param aSource abstract string reference containing source string |
michael@0 | 739 | * @param aSrcEncoding character encoding of the source string |
michael@0 | 740 | * @param aDest abstract string reference to hold the result |
michael@0 | 741 | */ |
michael@0 | 742 | XPCOM_API(nsresult) |
michael@0 | 743 | NS_CStringToUTF16(const nsACString &aSource, nsCStringEncoding aSrcEncoding, |
michael@0 | 744 | nsAString &aDest); |
michael@0 | 745 | |
michael@0 | 746 | /** |
michael@0 | 747 | * NS_UTF16ToCString |
michael@0 | 748 | * |
michael@0 | 749 | * This function converts the UTF-16 characters in a nsAString to a single-byte |
michael@0 | 750 | * encoding. The result is stored in a nsACString object. In some cases this |
michael@0 | 751 | * conversion may be lossy. In such cases, the conversion may succeed with a |
michael@0 | 752 | * return code indicating loss of information. The exact behavior is not |
michael@0 | 753 | * specified at this time. |
michael@0 | 754 | * |
michael@0 | 755 | * @param aSource abstract string reference containing source string |
michael@0 | 756 | * @param aDestEncoding character encoding of the resulting string |
michael@0 | 757 | * @param aDest abstract string reference to hold the result |
michael@0 | 758 | */ |
michael@0 | 759 | XPCOM_API(nsresult) |
michael@0 | 760 | NS_UTF16ToCString(const nsAString &aSource, nsCStringEncoding aDestEncoding, |
michael@0 | 761 | nsACString &aDest); |
michael@0 | 762 | |
michael@0 | 763 | #endif // nsXPCOMStrings_h__ |