2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2012-11-09 21:45:25 +04: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/. */
|
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#ifndef js_CharacterEncoding_h
|
|
|
|
#define js_CharacterEncoding_h
|
2012-11-09 21:45:25 +04:00
|
|
|
|
|
|
|
#include "mozilla/Range.h"
|
2019-09-26 15:45:00 +03:00
|
|
|
#include "mozilla/Span.h"
|
2012-11-09 21:45:25 +04:00
|
|
|
|
2013-09-27 02:34:54 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2013-08-14 02:34:12 +04:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
2019-10-14 12:32:07 +03:00
|
|
|
class JSLinearString;
|
2014-07-11 18:22:37 +04:00
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* By default, all C/C++ 1-byte-per-character strings passed into the JSAPI
|
|
|
|
* are treated as ISO/IEC 8859-1, also known as Latin-1. That is, each
|
|
|
|
* byte is treated as a 2-byte character, and there is no way to pass in a
|
|
|
|
* string containing characters beyond U+00FF.
|
|
|
|
*/
|
2014-05-31 12:44:32 +04:00
|
|
|
class Latin1Chars : public mozilla::Range<Latin1Char> {
|
|
|
|
typedef mozilla::Range<Latin1Char> Base;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = Latin1Char;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
Latin1Chars() : Base() {}
|
2015-03-29 01:22:11 +03:00
|
|
|
Latin1Chars(char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<Latin1Char*>(aBytes), aLength) {}
|
|
|
|
Latin1Chars(const Latin1Char* aBytes, size_t aLength)
|
|
|
|
: Base(const_cast<Latin1Char*>(aBytes), aLength) {}
|
|
|
|
Latin1Chars(const char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<Latin1Char*>(const_cast<char*>(aBytes)),
|
|
|
|
aLength) {}
|
2012-11-09 21:45:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A Latin1Chars, but with \0 termination for C compatibility.
|
|
|
|
*/
|
2014-05-31 12:44:32 +04:00
|
|
|
class Latin1CharsZ : public mozilla::RangedPtr<Latin1Char> {
|
|
|
|
typedef mozilla::RangedPtr<Latin1Char> Base;
|
2012-11-09 21:45:25 +04:00
|
|
|
|
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = Latin1Char;
|
|
|
|
|
2013-09-19 23:24:53 +04:00
|
|
|
Latin1CharsZ() : Base(nullptr, 0) {}
|
2012-11-09 21:45:25 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
Latin1CharsZ(char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<Latin1Char*>(aBytes), aLength) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(aBytes[aLength] == '\0');
|
2012-11-09 21:45:25 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
Latin1CharsZ(Latin1Char* aBytes, size_t aLength) : Base(aBytes, aLength) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(aBytes[aLength] == '\0');
|
2012-11-09 21:45:25 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 22:56:24 +04:00
|
|
|
using Base::operator=;
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
char* c_str() { return reinterpret_cast<char*>(get()); }
|
2012-11-09 21:45:25 +04:00
|
|
|
};
|
|
|
|
|
2013-07-10 10:17:32 +04:00
|
|
|
class UTF8Chars : public mozilla::Range<unsigned char> {
|
|
|
|
typedef mozilla::Range<unsigned char> Base;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-10 10:17:32 +04:00
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = unsigned char;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-10 10:17:32 +04:00
|
|
|
UTF8Chars() : Base() {}
|
2015-03-29 01:22:11 +03:00
|
|
|
UTF8Chars(char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<unsigned char*>(aBytes), aLength) {}
|
|
|
|
UTF8Chars(const char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<unsigned char*>(const_cast<char*>(aBytes)),
|
|
|
|
aLength) {}
|
2018-11-02 03:34:56 +03:00
|
|
|
UTF8Chars(mozilla::Utf8Unit* aUnits, size_t aLength)
|
|
|
|
: UTF8Chars(reinterpret_cast<char*>(aUnits), aLength) {}
|
|
|
|
UTF8Chars(const mozilla::Utf8Unit* aUnits, size_t aLength)
|
|
|
|
: UTF8Chars(reinterpret_cast<const char*>(aUnits), aLength) {}
|
2013-07-10 10:17:32 +04:00
|
|
|
};
|
|
|
|
|
2018-11-28 08:16:29 +03:00
|
|
|
/*
|
|
|
|
* Similar to UTF8Chars, but contains WTF-8.
|
|
|
|
* https://simonsapin.github.io/wtf-8/
|
|
|
|
*/
|
|
|
|
class WTF8Chars : public mozilla::Range<unsigned char> {
|
|
|
|
typedef mozilla::Range<unsigned char> Base;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-28 08:16:29 +03:00
|
|
|
public:
|
|
|
|
using CharT = unsigned char;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-28 08:16:29 +03:00
|
|
|
WTF8Chars() : Base() {}
|
|
|
|
WTF8Chars(char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<unsigned char*>(aBytes), aLength) {}
|
|
|
|
WTF8Chars(const char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<unsigned char*>(const_cast<char*>(aBytes)),
|
|
|
|
aLength) {}
|
|
|
|
};
|
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
/*
|
|
|
|
* SpiderMonkey also deals directly with UTF-8 encoded text in some places.
|
|
|
|
*/
|
|
|
|
class UTF8CharsZ : public mozilla::RangedPtr<unsigned char> {
|
|
|
|
typedef mozilla::RangedPtr<unsigned char> Base;
|
|
|
|
|
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = unsigned char;
|
|
|
|
|
2013-09-19 23:24:53 +04:00
|
|
|
UTF8CharsZ() : Base(nullptr, 0) {}
|
2012-11-09 21:45:25 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
UTF8CharsZ(char* aBytes, size_t aLength)
|
|
|
|
: Base(reinterpret_cast<unsigned char*>(aBytes), aLength) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(aBytes[aLength] == '\0');
|
2012-11-09 21:45:25 +04:00
|
|
|
}
|
2013-02-22 06:58:52 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
UTF8CharsZ(unsigned char* aBytes, size_t aLength) : Base(aBytes, aLength) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(aBytes[aLength] == '\0');
|
2013-02-22 06:58:52 +04:00
|
|
|
}
|
|
|
|
|
2018-11-02 03:34:56 +03:00
|
|
|
UTF8CharsZ(mozilla::Utf8Unit* aUnits, size_t aLength)
|
|
|
|
: UTF8CharsZ(reinterpret_cast<char*>(aUnits), aLength) {}
|
|
|
|
|
2013-12-19 22:56:24 +04:00
|
|
|
using Base::operator=;
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
char* c_str() { return reinterpret_cast<char*>(get()); }
|
2012-11-09 21:45:25 +04:00
|
|
|
};
|
|
|
|
|
2016-08-15 08:52:56 +03:00
|
|
|
/*
|
|
|
|
* A wrapper for a "const char*" that is encoded using UTF-8.
|
|
|
|
* This class does not manage ownership of the data; that is left
|
|
|
|
* to others. This differs from UTF8CharsZ in that the chars are
|
2018-09-05 11:25:12 +03:00
|
|
|
* const and it disallows assignment.
|
2016-08-15 08:52:56 +03:00
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_PUBLIC_API ConstUTF8CharsZ {
|
2016-08-15 08:52:56 +03:00
|
|
|
const char* data_;
|
|
|
|
|
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = unsigned char;
|
|
|
|
|
2016-08-15 08:52:56 +03:00
|
|
|
ConstUTF8CharsZ() : data_(nullptr) {}
|
|
|
|
|
|
|
|
ConstUTF8CharsZ(const char* aBytes, size_t aLength) : data_(aBytes) {
|
|
|
|
MOZ_ASSERT(aBytes[aLength] == '\0');
|
2016-08-13 17:03:30 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
validate(aLength);
|
|
|
|
#endif
|
2016-08-15 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const void* get() const { return data_; }
|
|
|
|
|
|
|
|
const char* c_str() const { return data_; }
|
|
|
|
|
|
|
|
explicit operator bool() const { return data_ != nullptr; }
|
2016-08-13 17:03:30 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
#ifdef DEBUG
|
|
|
|
void validate(size_t aLength);
|
|
|
|
#endif
|
2016-08-15 08:52:56 +03:00
|
|
|
};
|
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
/*
|
|
|
|
* SpiderMonkey uses a 2-byte character representation: it is a
|
|
|
|
* 2-byte-at-a-time view of a UTF-16 byte stream. This is similar to UCS-2,
|
|
|
|
* but unlike UCS-2, we do not strip UTF-16 extension bytes. This allows a
|
|
|
|
* sufficiently dedicated JavaScript program to be fully unicode-aware by
|
|
|
|
* manually interpreting UTF-16 extension characters embedded in the JS
|
|
|
|
* string.
|
|
|
|
*/
|
2014-07-22 08:43:21 +04:00
|
|
|
class TwoByteChars : public mozilla::Range<char16_t> {
|
|
|
|
typedef mozilla::Range<char16_t> Base;
|
2012-11-09 21:45:25 +04:00
|
|
|
|
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = char16_t;
|
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
TwoByteChars() : Base() {}
|
2015-03-29 01:22:11 +03:00
|
|
|
TwoByteChars(char16_t* aChars, size_t aLength) : Base(aChars, aLength) {}
|
|
|
|
TwoByteChars(const char16_t* aChars, size_t aLength)
|
|
|
|
: Base(const_cast<char16_t*>(aChars), aLength) {}
|
2012-11-09 21:45:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A TwoByteChars, but \0 terminated for compatibility with JSFlatString.
|
|
|
|
*/
|
2014-07-22 08:43:21 +04:00
|
|
|
class TwoByteCharsZ : public mozilla::RangedPtr<char16_t> {
|
|
|
|
typedef mozilla::RangedPtr<char16_t> Base;
|
2012-11-09 21:45:25 +04:00
|
|
|
|
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = char16_t;
|
|
|
|
|
2013-09-19 23:24:53 +04:00
|
|
|
TwoByteCharsZ() : Base(nullptr, 0) {}
|
2013-07-10 10:17:32 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
TwoByteCharsZ(char16_t* chars, size_t length) : Base(chars, length) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(chars[length] == '\0');
|
2012-11-09 21:45:25 +04:00
|
|
|
}
|
2013-12-19 22:56:24 +04:00
|
|
|
|
|
|
|
using Base::operator=;
|
2012-11-09 21:45:25 +04:00
|
|
|
};
|
|
|
|
|
2014-07-22 08:43:21 +04:00
|
|
|
typedef mozilla::RangedPtr<const char16_t> ConstCharPtr;
|
2014-01-31 02:58:53 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Like TwoByteChars, but the chars are const.
|
|
|
|
*/
|
2015-02-10 06:52:18 +03:00
|
|
|
class ConstTwoByteChars : public mozilla::Range<const char16_t> {
|
|
|
|
typedef mozilla::Range<const char16_t> Base;
|
2014-01-31 02:58:53 +04:00
|
|
|
|
2015-02-10 06:52:18 +03:00
|
|
|
public:
|
2016-08-13 17:03:31 +03:00
|
|
|
using CharT = char16_t;
|
|
|
|
|
2015-02-10 06:52:18 +03:00
|
|
|
ConstTwoByteChars() : Base() {}
|
2015-03-29 01:22:11 +03:00
|
|
|
ConstTwoByteChars(const char16_t* aChars, size_t aLength)
|
|
|
|
: Base(aChars, aLength) {}
|
2014-01-31 02:58:53 +04:00
|
|
|
};
|
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
/*
|
|
|
|
* Convert a 2-byte character sequence to "ISO-Latin-1". This works by
|
|
|
|
* truncating each 2-byte pair in the sequence to a 1-byte pair. If the source
|
|
|
|
* contains any UTF-16 extension characters, then this may give invalid Latin1
|
|
|
|
* output. The returned string is zero terminated. The returned string or the
|
|
|
|
* returned string's |start()| must be freed with JS_free or js_free,
|
|
|
|
* respectively. If allocation fails, an OOM error will be set and the method
|
2013-09-19 23:24:53 +04:00
|
|
|
* will return a nullptr chars (which can be tested for with the ! operator).
|
2012-11-09 21:45:25 +04:00
|
|
|
* This method cannot trigger GC.
|
|
|
|
*/
|
2017-02-02 22:12:43 +03:00
|
|
|
extern Latin1CharsZ LossyTwoByteCharsToNewLatin1CharsZ(
|
2014-07-22 08:43:21 +04:00
|
|
|
JSContext* cx, const mozilla::Range<const char16_t> tbchars);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
inline Latin1CharsZ LossyTwoByteCharsToNewLatin1CharsZ(JSContext* cx,
|
|
|
|
const char16_t* begin,
|
|
|
|
size_t length) {
|
2016-06-19 02:29:11 +03:00
|
|
|
const mozilla::Range<const char16_t> tbchars(begin, length);
|
|
|
|
return JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars);
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:18:23 +04:00
|
|
|
template <typename CharT>
|
2017-02-02 22:12:43 +03:00
|
|
|
extern UTF8CharsZ CharsToNewUTF8CharsZ(JSContext* maybeCx,
|
|
|
|
const mozilla::Range<CharT> chars);
|
2013-02-22 06:58:52 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS_PUBLIC_API uint32_t Utf8ToOneUcs4Char(const uint8_t* utf8Buffer,
|
|
|
|
int utf8Length);
|
2013-07-10 10:17:32 +04:00
|
|
|
|
|
|
|
/*
|
2014-07-22 08:43:21 +04:00
|
|
|
* Inflate bytes in UTF-8 encoding to char16_t.
|
2013-07-10 10:17:32 +04:00
|
|
|
* - On error, returns an empty TwoByteCharsZ.
|
|
|
|
* - On success, returns a malloc'd TwoByteCharsZ, and updates |outlen| to hold
|
|
|
|
* its length; the length value excludes the trailing null.
|
|
|
|
*/
|
2019-04-11 20:53:07 +03:00
|
|
|
extern JS_PUBLIC_API TwoByteCharsZ
|
|
|
|
UTF8CharsToNewTwoByteCharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen,
|
|
|
|
arena_id_t destArenaId);
|
2013-07-10 10:17:32 +04:00
|
|
|
|
2018-11-28 08:16:29 +03:00
|
|
|
/*
|
|
|
|
* Like UTF8CharsToNewTwoByteCharsZ, but for WTF8Chars.
|
|
|
|
*/
|
2019-04-11 20:53:07 +03:00
|
|
|
extern JS_PUBLIC_API TwoByteCharsZ
|
|
|
|
WTF8CharsToNewTwoByteCharsZ(JSContext* cx, const WTF8Chars wtf8, size_t* outlen,
|
|
|
|
arena_id_t destArenaId);
|
2018-11-28 08:16:29 +03:00
|
|
|
|
2016-08-15 08:52:56 +03:00
|
|
|
/*
|
|
|
|
* Like UTF8CharsToNewTwoByteCharsZ, but for ConstUTF8CharsZ.
|
|
|
|
*/
|
2019-04-11 20:53:07 +03:00
|
|
|
extern JS_PUBLIC_API TwoByteCharsZ
|
|
|
|
UTF8CharsToNewTwoByteCharsZ(JSContext* cx, const ConstUTF8CharsZ& utf8,
|
|
|
|
size_t* outlen, arena_id_t destArenaId);
|
2016-08-15 08:52:56 +03:00
|
|
|
|
2013-07-10 10:17:32 +04:00
|
|
|
/*
|
|
|
|
* The same as UTF8CharsToNewTwoByteCharsZ(), except that any malformed UTF-8
|
|
|
|
* characters will be replaced by \uFFFD. No exception will be thrown for
|
|
|
|
* malformed UTF-8 input.
|
|
|
|
*/
|
2019-04-11 20:53:07 +03:00
|
|
|
extern JS_PUBLIC_API TwoByteCharsZ
|
|
|
|
LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, const UTF8Chars utf8,
|
|
|
|
size_t* outlen, arena_id_t destArenaId);
|
2013-07-10 10:17:32 +04:00
|
|
|
|
2019-04-11 20:53:07 +03:00
|
|
|
extern JS_PUBLIC_API TwoByteCharsZ
|
|
|
|
LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, const ConstUTF8CharsZ& utf8,
|
|
|
|
size_t* outlen, arena_id_t destArenaId);
|
2016-08-15 08:52:56 +03:00
|
|
|
|
2014-07-11 18:22:37 +04:00
|
|
|
/*
|
|
|
|
* Returns the length of the char buffer required to encode |s| as UTF8.
|
|
|
|
* Does not include the null-terminator.
|
|
|
|
*/
|
2019-10-14 12:32:07 +03:00
|
|
|
JS_PUBLIC_API size_t GetDeflatedUTF8StringLength(JSLinearString* s);
|
2014-07-11 18:22:37 +04:00
|
|
|
|
|
|
|
/*
|
2019-09-26 15:45:00 +03:00
|
|
|
* Encode whole scalar values of |src| into |dst| as UTF-8 until |src| is
|
|
|
|
* exhausted or too little space is available in |dst| to fit the scalar
|
|
|
|
* value. Lone surrogates are converted to REPLACEMENT CHARACTER. Return
|
|
|
|
* the number of bytes of |dst| that were filled.
|
2019-09-18 11:28:24 +03:00
|
|
|
*
|
2019-09-26 15:45:00 +03:00
|
|
|
* Use |JS_EncodeStringToUTF8BufferPartial| if your string isn't already
|
2019-10-14 12:32:07 +03:00
|
|
|
* linear.
|
2016-05-17 18:08:48 +03:00
|
|
|
*
|
2019-10-14 12:32:07 +03:00
|
|
|
* Given |JSString* str = JS_FORGET_STRING_LINEARNESS(src)|,
|
2019-09-26 15:45:00 +03:00
|
|
|
* if |JS_StringHasLatin1Chars(str)|, then |src| is always fully converted
|
|
|
|
* if |dst.Length() >= JS_GetStringLength(str) * 2|. Otherwise |src| is
|
|
|
|
* always fully converted if |dst.Length() >= JS_GetStringLength(str) * 3|.
|
|
|
|
*
|
|
|
|
* The exact space required is always |GetDeflatedUTF8StringLength(str)|.
|
2014-07-11 18:22:37 +04:00
|
|
|
*/
|
2019-10-14 12:32:07 +03:00
|
|
|
JS_PUBLIC_API size_t DeflateStringToUTF8Buffer(JSLinearString* src,
|
2019-09-26 15:45:00 +03:00
|
|
|
mozilla::Span<char> dst);
|
2014-07-11 18:22:37 +04:00
|
|
|
|
2016-08-15 09:50:15 +03:00
|
|
|
/*
|
|
|
|
* The smallest character encoding capable of fully representing a particular
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
enum class SmallestEncoding { ASCII, Latin1, UTF16 };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the smallest encoding possible for the given string: if all
|
|
|
|
* codepoints are <128 then ASCII, otherwise if all codepoints are <256
|
|
|
|
* Latin-1, else UTF16.
|
|
|
|
*/
|
|
|
|
JS_PUBLIC_API SmallestEncoding FindSmallestEncoding(UTF8Chars utf8);
|
|
|
|
|
2016-08-13 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* Return a null-terminated Latin-1 string copied from the input string,
|
|
|
|
* storing its length (excluding null terminator) in |*outlen|. Fail and
|
|
|
|
* report an error if the string contains non-Latin-1 codepoints. Returns
|
|
|
|
* Latin1CharsZ() on failure.
|
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
extern JS_PUBLIC_API Latin1CharsZ
|
2019-04-11 20:53:07 +03:00
|
|
|
UTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8, size_t* outlen,
|
|
|
|
arena_id_t destArenaId);
|
2016-08-13 17:03:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a null-terminated Latin-1 string copied from the input string,
|
|
|
|
* storing its length (excluding null terminator) in |*outlen|. Non-Latin-1
|
|
|
|
* codepoints are replaced by '?'. Returns Latin1CharsZ() on failure.
|
|
|
|
*/
|
2019-04-11 20:53:07 +03:00
|
|
|
extern JS_PUBLIC_API Latin1CharsZ
|
|
|
|
LossyUTF8CharsToNewLatin1CharsZ(JSContext* cx, const UTF8Chars utf8,
|
|
|
|
size_t* outlen, arena_id_t destArenaId);
|
2016-08-13 17:03:31 +03:00
|
|
|
|
2016-09-30 06:34:43 +03:00
|
|
|
/*
|
|
|
|
* Returns true if all characters in the given null-terminated string are
|
|
|
|
* ASCII, i.e. < 0x80, false otherwise.
|
|
|
|
*/
|
|
|
|
extern JS_PUBLIC_API bool StringIsASCII(const char* s);
|
|
|
|
|
2019-09-28 07:24:46 +03:00
|
|
|
/*
|
|
|
|
* Returns true if all characters in the given span are ASCII,
|
|
|
|
* i.e. < 0x80, false otherwise.
|
|
|
|
*/
|
|
|
|
extern JS_PUBLIC_API bool StringIsASCII(mozilla::Span<const char> s);
|
|
|
|
|
2012-11-09 21:45:25 +04:00
|
|
|
} // namespace JS
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline void JS_free(JS::Latin1CharsZ& ptr) { js_free((void*)ptr.get()); }
|
|
|
|
inline void JS_free(JS::UTF8CharsZ& ptr) { js_free((void*)ptr.get()); }
|
2012-11-09 21:45:25 +04:00
|
|
|
|
2018-09-05 12:25:42 +03:00
|
|
|
/**
|
|
|
|
* DEPRECATED
|
|
|
|
*
|
|
|
|
* Allocate memory sufficient to contain the characters of |str| truncated to
|
|
|
|
* Latin-1 and a trailing null terminator, fill the memory with the characters
|
|
|
|
* interpreted in that manner plus the null terminator, and return a pointer to
|
|
|
|
* the memory.
|
|
|
|
*
|
|
|
|
* This function *loses information* when it copies the characters of |str| if
|
|
|
|
* |str| contains code units greater than 0xFF. Additionally, users that
|
|
|
|
* depend on null-termination will misinterpret the copied characters if |str|
|
|
|
|
* contains any nulls. Avoid using this function if possible, because it will
|
|
|
|
* eventually be removed.
|
|
|
|
*/
|
2018-09-05 16:05:03 +03:00
|
|
|
extern JS_PUBLIC_API JS::UniqueChars JS_EncodeStringToLatin1(JSContext* cx,
|
|
|
|
JSString* str);
|
2018-09-05 12:25:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DEPRECATED
|
|
|
|
*
|
2018-09-05 16:05:03 +03:00
|
|
|
* Same behavior as JS_EncodeStringToLatin1(), but encode into a UTF-8 string.
|
2018-09-05 12:25:42 +03:00
|
|
|
*
|
|
|
|
* This function *loses information* when it copies the characters of |str| if
|
|
|
|
* |str| contains invalid UTF-16: U+FFFD REPLACEMENT CHARACTER will be copied
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* The returned string is also subject to misinterpretation if |str| contains
|
|
|
|
* any nulls (which are faithfully transcribed into the returned string, but
|
|
|
|
* which will implicitly truncate the string if it's passed to functions that
|
|
|
|
* expect null-terminated strings).
|
|
|
|
*
|
|
|
|
* Avoid using this function if possible, because we'll remove it once we can
|
|
|
|
* devise a better API for the task.
|
|
|
|
*/
|
2018-11-19 20:02:47 +03:00
|
|
|
extern JS_PUBLIC_API JS::UniqueChars JS_EncodeStringToUTF8(
|
2018-09-05 12:25:42 +03:00
|
|
|
JSContext* cx, JS::Handle<JSString*> str);
|
|
|
|
|
2018-09-13 11:34:06 +03:00
|
|
|
/**
|
|
|
|
* DEPRECATED
|
|
|
|
*
|
|
|
|
* Same behavior as JS_EncodeStringToLatin1(), but encode into an ASCII string.
|
|
|
|
*
|
|
|
|
* This function asserts in debug mode that the input string contains only
|
|
|
|
* ASCII characters.
|
|
|
|
*
|
|
|
|
* The returned string is also subject to misinterpretation if |str| contains
|
|
|
|
* any nulls (which are faithfully transcribed into the returned string, but
|
|
|
|
* which will implicitly truncate the string if it's passed to functions that
|
|
|
|
* expect null-terminated strings).
|
|
|
|
*
|
|
|
|
* Avoid using this function if possible, because we'll remove it once we can
|
|
|
|
* devise a better API for the task.
|
|
|
|
*/
|
|
|
|
extern JS_PUBLIC_API JS::UniqueChars JS_EncodeStringToASCII(JSContext* cx,
|
|
|
|
JSString* str);
|
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#endif /* js_CharacterEncoding_h */
|