Added ArrayAutoPtr and reworked cstring -> String conversions

This commit is contained in:
waldemar%netscape.com 2000-02-02 08:50:26 +00:00
Родитель 94a4726044
Коммит 0639e1baad
4 изменённых файлов: 176 добавлений и 54 удалений

Просмотреть файл

@ -161,26 +161,6 @@ uint JS::floorLog2(uint32 n)
//
// Return a String containing the characters of the null-terminated C string cstr
// (without the trailing null).
JS::String JS::widenCString(const char *cstr)
{
size_t len = std::strlen(cstr);
String s(len, uni::null);
std::transform(cstr, cstr+len, s.begin(), widen);
return s;
}
// Widen and append length characters starting at chars to the end of str.
void JS::appendChars(String &str, const char *chars, size_t length)
{
String::size_type strLen = str.size();
str.append(length, uni::null);
std::transform(chars, chars+length, str.begin()+strLen, widen);
}
// Widen and append the null-terminated string cstr to the end of str.
// Return str.
JS::String &JS::operator+=(String &str, const char *cstr)
@ -193,8 +173,7 @@ JS::String &JS::operator+=(String &str, const char *cstr)
// Return the concatenation of str and the null-terminated string cstr.
JS::String JS::operator+(const String &str, const char *cstr)
{
String s = widenCString(cstr);
return str + s;
return str + widenCString(cstr);
}
@ -202,7 +181,8 @@ JS::String JS::operator+(const String &str, const char *cstr)
JS::String JS::operator+(const char *cstr, const String &str)
{
String s = widenCString(cstr);
return s += str;
s += str;
return s;
}
@ -1520,6 +1500,14 @@ bool JS::isASCIIHexDigit(char16 c, uint &digit)
}
// Return str advanced past white space characters, but no further than strEnd.
const char16 *JS::skipWhiteSpace(const char16 *str, const char16 *strEnd)
{
while (str != strEnd && isSpace(*str))
str++;
return str;
}
//
// C++ I/O
@ -1534,10 +1522,28 @@ JS::SaveFormat::~SaveFormat()
}
// Quotes for printing non-ASCII characters on an ASCII stream
#ifdef XP_MAC
const char beginUnprintable = char(0xC7);
const char endUnprintable = char(0xC8);
#else
const char beginUnprintable = '{';
const char endUnprintable = '}';
#endif
void JS::showChar(ostream &out, char16 ch)
{
SaveFormat sf(out);
out << '[' << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << (uint16)ch << ']';
#ifdef XP_MAC_MPW
if (ch == '\n')
out << '\r';
else
#endif
if (uchar16(ch) <= 0x7E && (uchar16(ch) >= ' ' || ch == '\n' || ch == '\t'))
out << static_cast<char>(ch);
else {
SaveFormat sf(out);
out << beginUnprintable << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << (uint16)ch << endUnprintable;
}
}

Просмотреть файл

@ -26,6 +26,7 @@
#include "systemtypes.h"
using std::size_t;
using std::ptrdiff_t;
using std::string;
using std::istream;
using std::ostream;
@ -76,8 +77,13 @@ namespace JavaScript {
// A string of UTF-16 characters. Nulls are allowed just like any other character.
// The string is not null-terminated.
// Use wstring if char16 is wchar_t. Otherwise use basic_string<uint16>.
//
// Eventually we'll want to use a custom class better suited for JavaScript that generates less
// code bloat and separates the concepts of a fixed, read-only string from a mutable buffer that
// is expanding. For now, though, we use the standard basic_string.
typedef std::basic_string<char16> String;
typedef uint32 char16orEOF; // A type that can hold any char16 plus one special value: ueof.
const char16orEOF char16eof = static_cast<char16orEOF>(-1);
@ -96,11 +102,29 @@ namespace JavaScript {
inline char16 widen(char ch) {return static_cast<char16>(static_cast<uchar>(ch));}
String widenCString(const char *cstr);
void appendChars(String &str, const char *chars, size_t length);
// Return a String containing the characters of the null-terminated C string cstr
// (without the trailing null).
inline String widenCString(const char *cstr)
{
size_t len = std::strlen(cstr);
const uchar *ucstr = reinterpret_cast<const uchar *>(cstr);
return String(ucstr, ucstr+len);
}
// Widen and append length characters starting at chars to the end of str.
inline void appendChars(String &str, const char *chars, size_t length)
{
const uchar *uchars = reinterpret_cast<const uchar *>(chars);
str.append(uchars, uchars + length);
}
String &operator+=(String &str, const char *cstr);
String operator+(const String &str, const char *cstr);
String operator+(const char *cstr, const String &str);
inline String &operator+=(String &str, char c) {return str += widen(c);}
inline void clear(String &s) {s.resize(0);}
class CharInfo {
@ -197,6 +221,37 @@ namespace JavaScript {
inline bool isASCIIDecimalDigit(char16 c) {return c >= '0' && c <= '9';}
bool isASCIIHexDigit(char16 c, uint &digit);
const char16 *skipWhiteSpace(const char16 *str, const char16 *strEnd);
//
// Array auto_ptr's
//
// An ArrayAutoPtr holds a pointer to an array initialized by new T[x].
// A regular auto_ptr cannot be used here because it deletes its pointer using
// delete rather than delete[].
// An appropriate operator[] is also provided.
template <typename T>
class ArrayAutoPtr {
T *ptr;
public:
explicit ArrayAutoPtr(T *p = 0): ptr(p) {}
ArrayAutoPtr(ArrayAutoPtr &a): ptr(a.ptr) {a.ptr = 0;}
ArrayAutoPtr &operator=(ArrayAutoPtr &a) {reset(a.release());}
~ArrayAutoPtr() {delete[] ptr;}
T &operator*() const {return *ptr;}
T &operator->() const {return *ptr;}
template<class N> T &operator[](N i) const {return ptr[i];}
T *get() const {return ptr;}
T *release() {T *p = ptr; ptr = 0; return p;}
void reset(T *p = 0) {delete[] ptr; ptr = p;}
};
typedef ArrayAutoPtr<char> CharAutoPtr;
//
// Growable arrays

Просмотреть файл

@ -161,26 +161,6 @@ uint JS::floorLog2(uint32 n)
//
// Return a String containing the characters of the null-terminated C string cstr
// (without the trailing null).
JS::String JS::widenCString(const char *cstr)
{
size_t len = std::strlen(cstr);
String s(len, uni::null);
std::transform(cstr, cstr+len, s.begin(), widen);
return s;
}
// Widen and append length characters starting at chars to the end of str.
void JS::appendChars(String &str, const char *chars, size_t length)
{
String::size_type strLen = str.size();
str.append(length, uni::null);
std::transform(chars, chars+length, str.begin()+strLen, widen);
}
// Widen and append the null-terminated string cstr to the end of str.
// Return str.
JS::String &JS::operator+=(String &str, const char *cstr)
@ -193,8 +173,7 @@ JS::String &JS::operator+=(String &str, const char *cstr)
// Return the concatenation of str and the null-terminated string cstr.
JS::String JS::operator+(const String &str, const char *cstr)
{
String s = widenCString(cstr);
return str + s;
return str + widenCString(cstr);
}
@ -202,7 +181,8 @@ JS::String JS::operator+(const String &str, const char *cstr)
JS::String JS::operator+(const char *cstr, const String &str)
{
String s = widenCString(cstr);
return s += str;
s += str;
return s;
}
@ -1520,6 +1500,14 @@ bool JS::isASCIIHexDigit(char16 c, uint &digit)
}
// Return str advanced past white space characters, but no further than strEnd.
const char16 *JS::skipWhiteSpace(const char16 *str, const char16 *strEnd)
{
while (str != strEnd && isSpace(*str))
str++;
return str;
}
//
// C++ I/O
@ -1534,10 +1522,28 @@ JS::SaveFormat::~SaveFormat()
}
// Quotes for printing non-ASCII characters on an ASCII stream
#ifdef XP_MAC
const char beginUnprintable = char(0xC7);
const char endUnprintable = char(0xC8);
#else
const char beginUnprintable = '{';
const char endUnprintable = '}';
#endif
void JS::showChar(ostream &out, char16 ch)
{
SaveFormat sf(out);
out << '[' << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << (uint16)ch << ']';
#ifdef XP_MAC_MPW
if (ch == '\n')
out << '\r';
else
#endif
if (uchar16(ch) <= 0x7E && (uchar16(ch) >= ' ' || ch == '\n' || ch == '\t'))
out << static_cast<char>(ch);
else {
SaveFormat sf(out);
out << beginUnprintable << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << (uint16)ch << endUnprintable;
}
}

Просмотреть файл

@ -26,6 +26,7 @@
#include "systemtypes.h"
using std::size_t;
using std::ptrdiff_t;
using std::string;
using std::istream;
using std::ostream;
@ -76,8 +77,13 @@ namespace JavaScript {
// A string of UTF-16 characters. Nulls are allowed just like any other character.
// The string is not null-terminated.
// Use wstring if char16 is wchar_t. Otherwise use basic_string<uint16>.
//
// Eventually we'll want to use a custom class better suited for JavaScript that generates less
// code bloat and separates the concepts of a fixed, read-only string from a mutable buffer that
// is expanding. For now, though, we use the standard basic_string.
typedef std::basic_string<char16> String;
typedef uint32 char16orEOF; // A type that can hold any char16 plus one special value: ueof.
const char16orEOF char16eof = static_cast<char16orEOF>(-1);
@ -96,11 +102,29 @@ namespace JavaScript {
inline char16 widen(char ch) {return static_cast<char16>(static_cast<uchar>(ch));}
String widenCString(const char *cstr);
void appendChars(String &str, const char *chars, size_t length);
// Return a String containing the characters of the null-terminated C string cstr
// (without the trailing null).
inline String widenCString(const char *cstr)
{
size_t len = std::strlen(cstr);
const uchar *ucstr = reinterpret_cast<const uchar *>(cstr);
return String(ucstr, ucstr+len);
}
// Widen and append length characters starting at chars to the end of str.
inline void appendChars(String &str, const char *chars, size_t length)
{
const uchar *uchars = reinterpret_cast<const uchar *>(chars);
str.append(uchars, uchars + length);
}
String &operator+=(String &str, const char *cstr);
String operator+(const String &str, const char *cstr);
String operator+(const char *cstr, const String &str);
inline String &operator+=(String &str, char c) {return str += widen(c);}
inline void clear(String &s) {s.resize(0);}
class CharInfo {
@ -197,6 +221,37 @@ namespace JavaScript {
inline bool isASCIIDecimalDigit(char16 c) {return c >= '0' && c <= '9';}
bool isASCIIHexDigit(char16 c, uint &digit);
const char16 *skipWhiteSpace(const char16 *str, const char16 *strEnd);
//
// Array auto_ptr's
//
// An ArrayAutoPtr holds a pointer to an array initialized by new T[x].
// A regular auto_ptr cannot be used here because it deletes its pointer using
// delete rather than delete[].
// An appropriate operator[] is also provided.
template <typename T>
class ArrayAutoPtr {
T *ptr;
public:
explicit ArrayAutoPtr(T *p = 0): ptr(p) {}
ArrayAutoPtr(ArrayAutoPtr &a): ptr(a.ptr) {a.ptr = 0;}
ArrayAutoPtr &operator=(ArrayAutoPtr &a) {reset(a.release());}
~ArrayAutoPtr() {delete[] ptr;}
T &operator*() const {return *ptr;}
T &operator->() const {return *ptr;}
template<class N> T &operator[](N i) const {return ptr[i];}
T *get() const {return ptr;}
T *release() {T *p = ptr; ptr = 0; return p;}
void reset(T *p = 0) {delete[] ptr; ptr = p;}
};
typedef ArrayAutoPtr<char> CharAutoPtr;
//
// Growable arrays