123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* 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/. */
- #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,
- const char16_t* aData,
- uint32_t aDataLength,
- uint32_t aFlags)
- {
- NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsString),
- "nsStringContainer is not large enough");
- if (!aData) {
- new (&aContainer) nsString();
- } else {
- if (aDataLength == UINT32_MAX) {
- if (NS_WARN_IF(aFlags & NS_STRING_CONTAINER_INIT_SUBSTRING)) {
- return NS_ERROR_INVALID_ARG;
- }
- aDataLength = nsCharTraits<char16_t>::length(aData);
- }
- if (aFlags & (NS_STRING_CONTAINER_INIT_DEPEND |
- NS_STRING_CONTAINER_INIT_ADOPT)) {
- uint32_t flags;
- 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;
- }
- new (&aContainer) nsSubstring(const_cast<char16_t*>(aData),
- aDataLength, flags);
- } else {
- new (&aContainer) nsString(aData, aDataLength);
- }
- }
- return NS_OK;
- }
- XPCOM_API(void)
- NS_StringContainerFinish(nsStringContainer& aContainer)
- {
- // call the nsString dtor
- reinterpret_cast<nsString*>(&aContainer)->~nsString();
- }
- /* ------------------------------------------------------------------------- */
- XPCOM_API(uint32_t)
- NS_StringGetData(const nsAString& aStr, const char16_t** aData,
- bool* aTerminated)
- {
- if (aTerminated) {
- *aTerminated = aStr.IsTerminated();
- }
- *aData = aStr.BeginReading();
- return aStr.Length();
- }
- XPCOM_API(uint32_t)
- NS_StringGetMutableData(nsAString& aStr, uint32_t aDataLength,
- char16_t** aData)
- {
- if (aDataLength != UINT32_MAX) {
- aStr.SetLength(aDataLength);
- if (aStr.Length() != aDataLength) {
- *aData = nullptr;
- return 0;
- }
- }
- *aData = aStr.BeginWriting();
- return aStr.Length();
- }
- XPCOM_API(char16_t*)
- NS_StringCloneData(const nsAString& aStr)
- {
- return ToNewUnicode(aStr);
- }
- XPCOM_API(nsresult)
- NS_StringSetData(nsAString& aStr, const char16_t* aData, uint32_t aDataLength)
- {
- aStr.Assign(aData, aDataLength);
- return NS_OK; // XXX report errors
- }
- XPCOM_API(nsresult)
- NS_StringSetDataRange(nsAString& aStr,
- uint32_t aCutOffset, uint32_t aCutLength,
- const char16_t* aData, uint32_t aDataLength)
- {
- if (aCutOffset == UINT32_MAX) {
- // append case
- if (aData) {
- aStr.Append(aData, aDataLength);
- }
- return NS_OK; // XXX report errors
- }
- if (aCutLength == UINT32_MAX) {
- aCutLength = aStr.Length() - aCutOffset;
- }
- if (aData) {
- if (aDataLength == UINT32_MAX) {
- aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData));
- } else {
- aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
- }
- } 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
- }
- XPCOM_API(void)
- NS_StringSetIsVoid(nsAString& aStr, const bool aIsVoid)
- {
- aStr.SetIsVoid(aIsVoid);
- }
- XPCOM_API(bool)
- NS_StringGetIsVoid(const nsAString& aStr)
- {
- return aStr.IsVoid();
- }
- /* ------------------------------------------------------------------------- */
- 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,
- uint32_t aDataLength,
- uint32_t aFlags)
- {
- NS_ASSERTION(sizeof(nsStringContainer_base) >= sizeof(nsCString),
- "nsStringContainer is not large enough");
- if (!aData) {
- new (&aContainer) nsCString();
- } else {
- if (aDataLength == UINT32_MAX) {
- if (NS_WARN_IF(aFlags & NS_CSTRING_CONTAINER_INIT_SUBSTRING)) {
- return NS_ERROR_INVALID_ARG;
- }
- aDataLength = nsCharTraits<char>::length(aData);
- }
- if (aFlags & (NS_CSTRING_CONTAINER_INIT_DEPEND |
- NS_CSTRING_CONTAINER_INIT_ADOPT)) {
- uint32_t flags;
- 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;
- }
- new (&aContainer) nsCSubstring(const_cast<char*>(aData),
- aDataLength, flags);
- } else {
- new (&aContainer) nsCString(aData, aDataLength);
- }
- }
- return NS_OK;
- }
- XPCOM_API(void)
- NS_CStringContainerFinish(nsCStringContainer& aContainer)
- {
- // call the nsCString dtor
- reinterpret_cast<nsCString*>(&aContainer)->~nsCString();
- }
- /* ------------------------------------------------------------------------- */
- XPCOM_API(uint32_t)
- NS_CStringGetData(const nsACString& aStr, const char** aData,
- bool* aTerminated)
- {
- if (aTerminated) {
- *aTerminated = aStr.IsTerminated();
- }
- *aData = aStr.BeginReading();
- return aStr.Length();
- }
- XPCOM_API(uint32_t)
- NS_CStringGetMutableData(nsACString& aStr, uint32_t aDataLength, char** aData)
- {
- if (aDataLength != UINT32_MAX) {
- aStr.SetLength(aDataLength);
- if (aStr.Length() != aDataLength) {
- *aData = nullptr;
- return 0;
- }
- }
- *aData = aStr.BeginWriting();
- return aStr.Length();
- }
- XPCOM_API(char*)
- NS_CStringCloneData(const nsACString& aStr)
- {
- return ToNewCString(aStr);
- }
- XPCOM_API(nsresult)
- NS_CStringSetData(nsACString& aStr, const char* aData, uint32_t aDataLength)
- {
- aStr.Assign(aData, aDataLength);
- return NS_OK; // XXX report errors
- }
- XPCOM_API(nsresult)
- NS_CStringSetDataRange(nsACString& aStr,
- uint32_t aCutOffset, uint32_t aCutLength,
- const char* aData, uint32_t aDataLength)
- {
- if (aCutOffset == UINT32_MAX) {
- // append case
- if (aData) {
- aStr.Append(aData, aDataLength);
- }
- return NS_OK; // XXX report errors
- }
- if (aCutLength == UINT32_MAX) {
- aCutLength = aStr.Length() - aCutOffset;
- }
- if (aData) {
- if (aDataLength == UINT32_MAX) {
- aStr.Replace(aCutOffset, aCutLength, nsDependentCString(aData));
- } else {
- aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
- }
- } 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
- }
- XPCOM_API(void)
- NS_CStringSetIsVoid(nsACString& aStr, const bool aIsVoid)
- {
- aStr.SetIsVoid(aIsVoid);
- }
- XPCOM_API(bool)
- NS_CStringGetIsVoid(const nsACString& aStr)
- {
- return aStr.IsVoid();
- }
- /* ------------------------------------------------------------------------- */
- 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
- }
|