2007-03-22 10:30:00 -07:00
|
|
|
/* vim:set ts=2 sw=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCharTraits.h"
|
|
|
|
|
|
|
|
#include "nsXPCOMStrings.h"
|
|
|
|
#include "nsNativeCharsetUtils.h"
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_StringContainerInit(nsStringContainer &aContainer)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsString),
|
|
|
|
"nsStringContainer is not large enough");
|
|
|
|
|
|
|
|
// use placement new to avoid heap allocating nsString object
|
|
|
|
new (&aContainer) nsString();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_StringContainerInit2(nsStringContainer &aContainer,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aData,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aDataLength,
|
|
|
|
uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsString),
|
|
|
|
"nsStringContainer is not large enough");
|
|
|
|
|
|
|
|
if (!aData)
|
|
|
|
{
|
|
|
|
new (&aContainer) nsString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aDataLength == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-01-04 07:02:17 -08:00
|
|
|
aDataLength = nsCharTraits<char16_t>::length(aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aFlags & (NS_STRING_CONTAINER_INIT_DEPEND |
|
|
|
|
NS_STRING_CONTAINER_INIT_ADOPT))
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING)
|
|
|
|
flags = nsSubstring::F_NONE;
|
|
|
|
else
|
|
|
|
flags = nsSubstring::F_TERMINATED;
|
|
|
|
|
|
|
|
if (aFlags & NS_STRING_CONTAINER_INIT_ADOPT)
|
|
|
|
flags |= nsSubstring::F_OWNED;
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
new (&aContainer) nsSubstring(const_cast<char16_t *>(aData),
|
2007-03-22 10:30:00 -07:00
|
|
|
aDataLength, flags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new (&aContainer) nsString(aData, aDataLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(void)
|
|
|
|
NS_StringContainerFinish(nsStringContainer &aContainer)
|
|
|
|
{
|
|
|
|
// call the nsString dtor
|
2007-07-08 00:08:04 -07:00
|
|
|
reinterpret_cast<nsString *>(&aContainer)->~nsString();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
XPCOM_API(uint32_t)
|
2014-01-04 07:02:17 -08:00
|
|
|
NS_StringGetData(const nsAString &aStr, const char16_t **aData,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aTerminated)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aTerminated)
|
|
|
|
*aTerminated = aStr.IsTerminated();
|
|
|
|
|
|
|
|
nsAString::const_iterator begin;
|
|
|
|
aStr.BeginReading(begin);
|
|
|
|
*aData = begin.get();
|
|
|
|
return begin.size_forward();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
XPCOM_API(uint32_t)
|
|
|
|
NS_StringGetMutableData(nsAString &aStr, uint32_t aDataLength,
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t **aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aDataLength != UINT32_MAX) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aStr.SetLength(aDataLength);
|
|
|
|
if (aStr.Length() != aDataLength) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aData = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAString::iterator begin;
|
|
|
|
aStr.BeginWriting(begin);
|
|
|
|
*aData = begin.get();
|
|
|
|
return begin.size_forward();
|
|
|
|
}
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
XPCOM_API(char16_t *)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_StringCloneData(const nsAString &aStr)
|
|
|
|
{
|
|
|
|
return ToNewUnicode(aStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
2014-01-04 07:02:17 -08:00
|
|
|
NS_StringSetData(nsAString &aStr, const char16_t *aData, uint32_t aDataLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aStr.Assign(aData, aDataLength);
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_StringSetDataRange(nsAString &aStr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCutOffset, uint32_t aCutLength,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aData, uint32_t aDataLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aCutOffset == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// append case
|
|
|
|
if (aData)
|
|
|
|
aStr.Append(aData, aDataLength);
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aCutLength == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
aCutLength = aStr.Length() - aCutOffset;
|
|
|
|
|
|
|
|
if (aData)
|
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aDataLength == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData));
|
|
|
|
else
|
2011-12-09 00:35:41 -08:00
|
|
|
aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
aStr.Cut(aCutOffset, aCutLength);
|
|
|
|
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_StringCopy(nsAString &aDest, const nsAString &aSrc)
|
|
|
|
{
|
|
|
|
aDest.Assign(aSrc);
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
2007-08-17 20:48:16 -07:00
|
|
|
XPCOM_API(void)
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_StringSetIsVoid(nsAString &aStr, const bool aIsVoid)
|
2007-08-17 20:48:16 -07:00
|
|
|
{
|
|
|
|
aStr.SetIsVoid(aIsVoid);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
XPCOM_API(bool)
|
2007-08-17 20:48:16 -07:00
|
|
|
NS_StringGetIsVoid(const nsAString &aStr)
|
|
|
|
{
|
|
|
|
return aStr.IsVoid();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_CStringContainerInit(nsCStringContainer &aContainer)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsCString),
|
|
|
|
"nsCStringContainer is not large enough");
|
|
|
|
|
|
|
|
// use placement new to avoid heap allocating nsCString object
|
|
|
|
new (&aContainer) nsCString();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_CStringContainerInit2(nsCStringContainer &aContainer,
|
|
|
|
const char *aData,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aDataLength,
|
|
|
|
uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsCString),
|
|
|
|
"nsStringContainer is not large enough");
|
|
|
|
|
|
|
|
if (!aData)
|
|
|
|
{
|
|
|
|
new (&aContainer) nsCString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aDataLength == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-19 13:27:37 -08:00
|
|
|
if (NS_WARN_IF(aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING))
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-03-22 10:30:00 -07:00
|
|
|
aDataLength = nsCharTraits<char>::length(aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFlags & (NS_CSTRING_CONTAINER_INIT_DEPEND |
|
|
|
|
NS_CSTRING_CONTAINER_INIT_ADOPT))
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING)
|
|
|
|
flags = nsCSubstring::F_NONE;
|
|
|
|
else
|
|
|
|
flags = nsCSubstring::F_TERMINATED;
|
|
|
|
|
|
|
|
if (aFlags & NS_CSTRING_CONTAINER_INIT_ADOPT)
|
|
|
|
flags |= nsCSubstring::F_OWNED;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
new (&aContainer) nsCSubstring(const_cast<char *>(aData),
|
2007-03-22 10:30:00 -07:00
|
|
|
aDataLength, flags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new (&aContainer) nsCString(aData, aDataLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(void)
|
|
|
|
NS_CStringContainerFinish(nsCStringContainer &aContainer)
|
|
|
|
{
|
|
|
|
// call the nsCString dtor
|
2007-07-08 00:08:04 -07:00
|
|
|
reinterpret_cast<nsCString *>(&aContainer)->~nsCString();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
XPCOM_API(uint32_t)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_CStringGetData(const nsACString &aStr, const char **aData,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aTerminated)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aTerminated)
|
|
|
|
*aTerminated = aStr.IsTerminated();
|
|
|
|
|
|
|
|
nsACString::const_iterator begin;
|
|
|
|
aStr.BeginReading(begin);
|
|
|
|
*aData = begin.get();
|
|
|
|
return begin.size_forward();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
XPCOM_API(uint32_t)
|
|
|
|
NS_CStringGetMutableData(nsACString &aStr, uint32_t aDataLength, char **aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aDataLength != UINT32_MAX) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aStr.SetLength(aDataLength);
|
|
|
|
if (aStr.Length() != aDataLength) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aData = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsACString::iterator begin;
|
|
|
|
aStr.BeginWriting(begin);
|
|
|
|
*aData = begin.get();
|
|
|
|
return begin.size_forward();
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(char *)
|
|
|
|
NS_CStringCloneData(const nsACString &aStr)
|
|
|
|
{
|
|
|
|
return ToNewCString(aStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_CStringSetData(nsACString &aStr, const char *aData, uint32_t aDataLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aStr.Assign(aData, aDataLength);
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_CStringSetDataRange(nsACString &aStr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCutOffset, uint32_t aCutLength,
|
|
|
|
const char *aData, uint32_t aDataLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aCutOffset == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// append case
|
|
|
|
if (aData)
|
|
|
|
aStr.Append(aData, aDataLength);
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aCutLength == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
aCutLength = aStr.Length() - aCutOffset;
|
|
|
|
|
|
|
|
if (aData)
|
|
|
|
{
|
2012-09-27 23:57:33 -07:00
|
|
|
if (aDataLength == UINT32_MAX)
|
2007-03-22 10:30:00 -07:00
|
|
|
aStr.Replace(aCutOffset, aCutLength, nsDependentCString(aData));
|
|
|
|
else
|
2011-12-09 00:35:41 -08:00
|
|
|
aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
aStr.Cut(aCutOffset, aCutLength);
|
|
|
|
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_CStringCopy(nsACString &aDest, const nsACString &aSrc)
|
|
|
|
{
|
|
|
|
aDest.Assign(aSrc);
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
2007-08-17 20:48:16 -07:00
|
|
|
XPCOM_API(void)
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_CStringSetIsVoid(nsACString &aStr, const bool aIsVoid)
|
2007-08-17 20:48:16 -07:00
|
|
|
{
|
|
|
|
aStr.SetIsVoid(aIsVoid);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
XPCOM_API(bool)
|
2007-08-17 20:48:16 -07:00
|
|
|
NS_CStringGetIsVoid(const nsACString &aStr)
|
|
|
|
{
|
|
|
|
return aStr.IsVoid();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_CStringToUTF16(const nsACString &aSrc,
|
|
|
|
nsCStringEncoding aSrcEncoding,
|
|
|
|
nsAString &aDest)
|
|
|
|
{
|
|
|
|
switch (aSrcEncoding)
|
|
|
|
{
|
|
|
|
case NS_CSTRING_ENCODING_ASCII:
|
|
|
|
CopyASCIItoUTF16(aSrc, aDest);
|
|
|
|
break;
|
|
|
|
case NS_CSTRING_ENCODING_UTF8:
|
|
|
|
CopyUTF8toUTF16(aSrc, aDest);
|
|
|
|
break;
|
|
|
|
case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
|
|
|
|
NS_CopyNativeToUnicode(aSrc, aDest);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(nsresult)
|
|
|
|
NS_UTF16ToCString(const nsAString &aSrc,
|
|
|
|
nsCStringEncoding aDestEncoding,
|
|
|
|
nsACString &aDest)
|
|
|
|
{
|
|
|
|
switch (aDestEncoding)
|
|
|
|
{
|
|
|
|
case NS_CSTRING_ENCODING_ASCII:
|
|
|
|
LossyCopyUTF16toASCII(aSrc, aDest);
|
|
|
|
break;
|
|
|
|
case NS_CSTRING_ENCODING_UTF8:
|
|
|
|
CopyUTF16toUTF8(aSrc, aDest);
|
|
|
|
break;
|
|
|
|
case NS_CSTRING_ENCODING_NATIVE_FILESYSTEM:
|
|
|
|
NS_CopyUnicodeToNative(aSrc, aDest);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK; // XXX report errors
|
|
|
|
}
|