#ifndef _KJS_USTRING_H_
#define _KJS_USTRING_H_
#include "JSLock.h"
#include "collector.h"
#include <stdint.h>
#include <wtf/Assertions.h>
#include <wtf/FastMalloc.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
#if COMPILER(GCC) && PLATFORM(FORCE_PACK)
#define PACK_STRUCT __attribute__((packed))
#else
#define PACK_STRUCT
#endif
namespace DOM {
class DOMString;
class AtomicString;
}
class KJScript;
namespace KJS {
using WTF::PlacementNewAdoptType;
using WTF::PlacementNewAdopt;
class UString;
struct UChar {
UChar();
UChar(unsigned char h , unsigned char l);
UChar(char u);
UChar(unsigned char u);
UChar(unsigned short u);
unsigned char high() const { return static_cast<unsigned char>(uc >> 8); }
unsigned char low() const { return static_cast<unsigned char>(uc); }
unsigned short unicode() const { return uc; }
unsigned short uc;
} PACK_STRUCT;
inline UChar::UChar() { }
inline UChar::UChar(unsigned char h , unsigned char l) : uc(h << 8 | l) { }
inline UChar::UChar(char u) : uc((unsigned char)u) { }
inline UChar::UChar(unsigned char u) : uc(u) { }
inline UChar::UChar(unsigned short u) : uc(u) { }
class CString {
public:
CString() : data(0), length(0) { }
CString(const char *c);
CString(const char *c, size_t len);
CString(const CString &);
~CString();
CString &append(const CString &);
CString &operator=(const char *c);
CString &operator=(const CString &);
CString &operator+=(const CString &c) { return append(c); }
size_t size() const { return length; }
const char *c_str() const { return data; }
private:
char *data;
size_t length;
};
class UString {
friend bool operator==(const UString&, const UString&);
public:
struct Rep {
static PassRefPtr<Rep> create(UChar *d, int l);
static PassRefPtr<Rep> createCopying(const UChar *d, int l);
static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
void destroy();
bool baseIsSelf() const { return baseString == this; }
UChar* data() const { return baseString->buf + baseString->preCapacity + offset; }
int size() const { return len; }
unsigned hash() const { if (_hash == 0) _hash = computeHash(data(), len); return _hash; }
unsigned computedHash() const { ASSERT(_hash); return _hash; }
static unsigned computeHash(const UChar *, int length);
static unsigned computeHash(const char *);
Rep* ref() { ASSERT(JSLock::lockCount() > 0); ++rc; return this; }
ALWAYS_INLINE void deref() { ASSERT(JSLock::lockCount() > 0); if (--rc == 0) destroy(); }
int offset;
int len;
int rc;
mutable unsigned _hash;
bool isIdentifier;
UString::Rep* baseString;
size_t reportedCost;
UChar *buf;
int usedCapacity;
int capacity;
int usedPreCapacity;
int preCapacity;
static Rep null;
static Rep empty;
};
public:
UString();
UString(const char *c);
UString(const UChar *c, int length);
UString(UChar *c, int length, bool copy);
UString(const UString &s) : m_rep(s.m_rep) {}
UString(const Vector<UChar>& buffer);
UString(const DOM::DOMString&);
UString(const DOM::AtomicString&);
UString(const UString &, const UString &);
~UString() {}
UString(PlacementNewAdoptType) : m_rep(PlacementNewAdopt) { }
static UString from(int i);
static UString from(unsigned int u);
static UString from(long u);
static UString from(double d);
struct Range {
public:
Range(int pos, int len) : position(pos), length(len) {}
Range() {}
int position;
int length;
};
UString spliceSubstringsWithSeparators(const Range *substringRanges, int rangeCount, const UString *separators, int separatorCount) const;
UString &append(const UString &);
UString &append(const char *);
UString &append(unsigned short);
UString &append(char c) { return append(static_cast<unsigned short>(static_cast<unsigned char>(c))); }
UString &append(UChar c) { return append(c.uc); }
CString cstring() const;
char *ascii() const;
CString UTF8String(bool strict = false) const;
DOM::DOMString domString() const;
UString &operator=(const char *c);
UString &operator+=(const UString &s) { return append(s); }
UString &operator+=(const char *s) { return append(s); }
const UChar* data() const { return m_rep->data(); }
bool isNull() const { return (m_rep == &Rep::null); }
bool isEmpty() const { return (!m_rep->len); }
bool is8Bit() const;
int size() const { return m_rep->size(); }
const UChar operator[](int pos) const;
double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
double toDouble(bool tolerateTrailingJunk) const;
double toDouble() const;
uint32_t toUInt32(bool *ok = 0) const;
uint32_t toUInt32(bool *ok, bool tolerateEmptyString) const;
uint32_t toStrictUInt32(bool *ok = 0) const;
unsigned toArrayIndex(bool *ok = 0) const;
int find(const UString &f, int pos = 0) const;
int find(UChar, int pos = 0) const;
int rfind(const UString &f, int pos) const;
int rfind(UChar, int pos) const;
UString substr(int pos = 0, int len = -1) const;
static const UString &null();
Rep* rep() const { return m_rep.get(); }
UString(PassRefPtr<Rep> r) : m_rep(r) { ASSERT(m_rep); }
size_t cost() const;
private:
size_t expandedSize(size_t size, size_t otherSize) const;
int usedCapacity() const;
int usedPreCapacity() const;
void expandCapacity(int requiredLength);
void expandPreCapacity(int requiredPreCap);
RefPtr<Rep> m_rep;
};
inline bool operator==(const UChar &c1, const UChar &c2) {
return (c1.uc == c2.uc);
}
bool operator==(const UString& s1, const UString& s2);
inline bool operator!=(const UString& s1, const UString& s2) {
return !KJS::operator==(s1, s2);
}
bool operator<(const UString& s1, const UString& s2);
bool operator==(const UString& s1, const char *s2);
inline bool operator!=(const UString& s1, const char *s2) {
return !KJS::operator==(s1, s2);
}
inline bool operator==(const char *s1, const UString& s2) {
return operator==(s2, s1);
}
inline bool operator!=(const char *s1, const UString& s2) {
return !KJS::operator==(s1, s2);
}
bool operator==(const CString& s1, const CString& s2);
inline UString operator+(const UString& s1, const UString& s2) {
return UString(s1, s2);
}
int compare(const UString &, const UString &);
inline UString::UString()
: m_rep(&Rep::null)
{
}
inline unsigned UString::toArrayIndex(bool *ok) const
{
unsigned i = toStrictUInt32(ok);
if (ok && i >= 0xFFFFFFFFU)
*ok = false;
return i;
}
static const int minShareSize = Collector::minExtraCostSize / sizeof(UChar);
inline size_t UString::cost() const
{
size_t capacity = (m_rep->baseString->capacity + m_rep->baseString->preCapacity) * sizeof(UChar);
size_t reportedCost = m_rep->baseString->reportedCost;
ASSERT(capacity >= reportedCost);
size_t capacityDelta = capacity - reportedCost;
if (capacityDelta < static_cast<size_t>(minShareSize))
return 0;
m_rep->baseString->reportedCost = capacity;
return capacityDelta;
}
}
#endif