void test_Compare(void) {
	STRING_TYPE a = getString("a");
	STRING_TYPE b = getString("ab");
	STRING_TYPE c = getString("b");
	STRING_TYPE d = getString("a");

	CU_ASSERT_EQUAL(Compare(a, a), 0);
	CU_ASSERT_EQUAL(Compare(a, b), -1);
	CU_ASSERT_EQUAL(Compare(a, c), -1);

	CU_ASSERT_EQUAL(Compare(b, a), 1);
	CU_ASSERT_EQUAL(Compare(b, b), 0);
	CU_ASSERT_EQUAL(Compare(b, c), -1);

	CU_ASSERT_EQUAL(Compare(c, a), 1);
	CU_ASSERT_EQUAL(Compare(c, b), 1);
	CU_ASSERT_EQUAL(Compare(c, c), 0);

	CU_ASSERT_EQUAL(Compare(a, d), 0);

	CU_ASSERT_EQUAL(Compare(a, NULL), 1);
	CU_ASSERT_EQUAL(Compare(NULL, a), -1);

	CU_ASSERT_EQUAL(Compare(a, getEmptyString()), 1);
	CU_ASSERT_EQUAL(Compare(getEmptyString(), a), -1);
}
Esempio n. 2
0
static char16_t* allocFromUTF8(const char* u8str, size_t u8len)
{
    if (u8len == 0) return getEmptyString();

    const uint8_t* u8cur = (const uint8_t*) u8str;

    const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
    if (u16len < 0) {
        return getEmptyString();
    }

    const uint8_t* const u8end = u8cur + u8len;

    SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1));
    if (buf) {
        u8cur = (const uint8_t*) u8str;
        char16_t* u16str = (char16_t*)buf->data();

        utf8_to_utf16(u8cur, u8len, u16str);

        //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
        //printHexData(1, str, buf->size(), 16, 1);
        //printf("\n");
        
        return u16str;
    }

    return getEmptyString();
}
Esempio n. 3
0
static char* allocFromUTF16OrUTF32(const T* in, L len)
{
    if (len == 0) return getEmptyString();

    size_t bytes = 0;
    const T* end = in+len;
    const T* p = in;

    while (p < end) {
        bytes += utf32_to_utf8_bytes(*p);
        p++;
    }

    SharedBuffer* buf = SharedBuffer::alloc(bytes+1);
    LOG_ASSERT(buf, "Unable to allocate shared buffer");
    if (buf) {
        p = in;
        char* str = (char*)buf->data();
        char* d = str;
        while (p < end) {
            const T c = *p++;
            size_t len = utf32_to_utf8_bytes(c);
            utf32_to_utf8((uint8_t*)d, c, len);
            d += len;
        }
        *d = 0;

        return str;
    }

    return getEmptyString();
}
Esempio n. 4
0
status_t String16::remove(size_t len, size_t begin)
{
    const size_t N = size();
    if (begin >= N) {
        SharedBuffer::bufferFromData(mString)->release();
        mString = getEmptyString();
        return NO_ERROR;
    }
    if ((begin+len) > N) len = N-begin;
    if (begin == 0 && len == N) {
        return NO_ERROR;
    }

    if (begin > 0) {
        SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
            ->editResize((N+1)*sizeof(char16_t));
        if (!buf) {
            return NO_MEMORY;
        }
        char16_t* str = (char16_t*)buf->data();
        memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
        mString = str;
    }
    SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
        ->editResize((len+1)*sizeof(char16_t));
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        str[len] = 0;
        mString = str;
        return NO_ERROR;
    }
    return NO_MEMORY;
}
Esempio n. 5
0
	String8::String8(const char* o)
		: mString(allocFromUTF8(o, strlen(o)))
	{
		if (mString == NULL) {
			mString = getEmptyString();
		}
	}
Esempio n. 6
0
	String8::String8(const char* o, size_t len)
		: mString(allocFromUTF8(o, len))
	{
		if (mString == NULL) {
			mString = getEmptyString();
		}
	}
Esempio n. 7
0
void SimpleString::replace(const char* to, const char* with)
{
    size_t c = count(to);
    size_t len = size();
    size_t tolen = StrLen(to);
    size_t withlen = StrLen(with);

    size_t newsize = len + (withlen * c) - (tolen * c) + 1;

    if (newsize > 1) {
        char* newbuf = allocStringBuffer(newsize, __FILE__, __LINE__);
        for (size_t i = 0, j = 0; i < len;) {
            if (StrNCmp(&buffer_[i], to, tolen) == 0) {
                StrNCpy(&newbuf[j], with, withlen + 1);
                j += withlen;
                i += tolen;
            }
            else {
                newbuf[j] = buffer_[i];
                j++;
                i++;
            }
        }
        deallocStringBuffer(buffer_, __FILE__, __LINE__);
        buffer_ = newbuf;
        buffer_[newsize - 1] = '\0';
    }
    else {
        deallocStringBuffer(buffer_, __FILE__, __LINE__);
        buffer_ = getEmptyString();
    }
}
void test_Copy(void) {
	STRING_TYPE string = getTestString(), copied = NULL;

	CU_ASSERT_EQUAL(Copy(&copied, string), OK);

	CU_ASSERT_EQUAL(Compare(string, copied), 0);
	CU_ASSERT_NOT_EQUAL(Compare(copied, getEmptyString()), 0);
}
void test_Concat(void) {
	STRING_TYPE temp = NULL;

	CU_ASSERT_EQUAL(Concat(&temp, getString("abc"), getString("de")), OK);
	CU_ASSERT_EQUAL(Compare(temp, getString("abcde")), 0);

	CU_ASSERT_EQUAL(Concat(&temp, getString("abc"), getEmptyString()), OK);
	CU_ASSERT_EQUAL(Compare(temp, getString("abc")), 0);
}
Esempio n. 10
0
status_t String8::setTo(const char* other)
{
    SharedBuffer::bufferFromData(mString)->release();
    mString = allocFromUTF8(other, strlen(other));
    if (mString) return NO_ERROR;

    mString = getEmptyString();
    return NO_MEMORY;
}
Esempio n. 11
0
SimpleString::SimpleString(const char *otherBuffer)
{
    if (otherBuffer == 0) {
        buffer_ = getEmptyString();
    }
    else {
        buffer_ = copyToNewBuffer(otherBuffer);
    }
}
Esempio n. 12
0
static char* allocFromUTF16(const char16_t* in, size_t len)
{
    if (len == 0) return getEmptyString();

    const size_t bytes = utf8_length_from_utf16(in, len);

    SharedBuffer* buf = SharedBuffer::alloc(bytes+1);
    LOG_ASSERT(buf, "Unable to allocate shared buffer");
    if (buf) {
        char* str = (char*)buf->data();

        utf16_to_utf8(in, len, str, bytes+1);

        return str;
    }

    return getEmptyString();
}
Esempio n. 13
0
status_t String8::setTo(const char32_t* other, size_t len)
{
    SharedBuffer::bufferFromData(mString)->release();
    mString = allocFromUTF32(other, len);
    if (mString) return NO_ERROR;

    mString = getEmptyString();
    return NO_MEMORY;
}
Esempio n. 14
0
static char* allocFromUTF16(const char16_t* in, size_t len)
{
    if (len == 0) return getEmptyString();

    const ssize_t bytes = utf16_to_utf8_length(in, len);
    if (bytes < 0) {
        return getEmptyString();
    }

    SharedBuffer* buf = SharedBuffer::alloc(bytes+1);
    if (!buf) {
        return getEmptyString();
    }

    char* str = (char*)buf->data();
    utf16_to_utf8(in, len, str);
    return str;
}
Esempio n. 15
0
 const std::string& Hasher::retrieveValue(std::size_t hash)
 {
   if(!hasValue(hash))
     return getEmptyString();
 
   std::map<std::size_t, std::string>::iterator it = getMap().find(hash);
 
   return it->second;
 }
SimpleString::SimpleString(const char *otherBuffer)
{
	if (otherBuffer == 0) {
		buffer_ = getEmptyString();
	}
	else {
		size_t len = PlatformSpecificStrLen(otherBuffer) + 1;
		buffer_ = allocStringBuffer(len);
		PlatformSpecificStrCpy(buffer_, otherBuffer);
	}
}
Esempio n. 17
0
static char* allocFromUTF16(const char16_t* in, size_t len)
{
    if (len == 0) return getEmptyString();

     // Allow for closing '\0'
    const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1;
    if (resultStrLen < 1) {
        return getEmptyString();
    }

    SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
    ALOG_ASSERT(buf, "Unable to allocate shared buffer");
    if (!buf) {
        return getEmptyString();
    }

    char* resultStr = (char*)buf->data();
    utf16_to_utf8(in, len, resultStr, resultStrLen);
    return resultStr;
}
static char* allocFromUTF32(const char32_t* in, size_t len)
{
    if (len == 0) {
        return getEmptyString();
    }

    const ssize_t bytes = utf32_to_utf8_length(in, len);
    if (bytes < 0) {
        return getEmptyString();
    }

    SharedBuffer* buf = SharedBuffer::alloc(bytes+1);
    ALOG_ASSERT(buf, "Unable to allocate shared buffer");
    if (!buf) {
        return getEmptyString();
    }

    char* str = (char*) buf->data();
    utf32_to_utf8(in, len, str);

    return str;
}
Esempio n. 19
0
String16::String16(const char16_t* o, size_t len)
{
    SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
    ALOG_ASSERT(buf, "Unable to allocate shared buffer");
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        memcpy(str, o, len*sizeof(char16_t));
        str[len] = 0;
        mString = str;
        return;
    }
    
    mString = getEmptyString();
}
Esempio n. 20
0
String16::String16(const char16_t* o)
{
    size_t len = strlen16(o);
    SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
    ALOG_ASSERT(buf, "Unable to allocate shared buffer");
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        strcpy16(str, o);
        mString = str;
        return;
    }
    
    mString = getEmptyString();
}
Esempio n. 21
0
static char* allocFromUTF8(const char* in, size_t len)
{
    if (len > 0) {
        SharedBuffer* buf = SharedBuffer::alloc(len+1);
        if (buf) {
            char* str = (char*)buf->data();
            memcpy(str, in, len);
            str[len] = 0;
            return str;
        }
        return NULL;
    }

    return getEmptyString();
}
Esempio n. 22
0
void test_Replace(void) {
	STRING_TYPE a = getString("abc");
	STRING_TYPE b = getString("de");
	STRING_TYPE c = getString("abcdeabc");

	//replace a by b
	c = getString("abcdeabc");
	CU_ASSERT_EQUAL(Replace(&c, a, b), OK);
	CU_ASSERT_EQUAL(Compare(c, getString("dedede")), 0);

	//replace a by empty
	c = getString("abcdeabc");
	CU_ASSERT_EQUAL(Replace(&c, a, getEmptyString()), OK);
	CU_ASSERT_EQUAL(Compare(c, getString("de")), 0);
}
Esempio n. 23
0
void test_SubString(void) {
	STRING_TYPE a = getString("abcde"), temp = NULL;

	CU_ASSERT_EQUAL(SubString(&temp, a, 1, 3), OK);
	CU_ASSERT_EQUAL(Compare(temp, getString("abc")), 0);

	CU_ASSERT_EQUAL(SubString(&temp, a, 4, 2), OK);
	CU_ASSERT_EQUAL(Compare(temp, getString("de")), 0);

	CU_ASSERT_EQUAL(SubString(&temp, a, 4, 0), OK);
	CU_ASSERT_EQUAL(Compare(temp, getEmptyString()), 0);

	CU_ASSERT_EQUAL(SubString(&temp, a, 0, 1), ERROR);
	CU_ASSERT_EQUAL(SubString(&temp, a, 1, 10), ERROR);
	CU_ASSERT_EQUAL(SubString(&temp, a, 10, 1), ERROR);
}
Esempio n. 24
0
errorCode createUriTableEntries(UriTable* uriTable, boolean withSchema)
{
	errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
	String emptyStr;
	getEmptyString(&emptyStr);

	// See http://www.w3.org/TR/exi/#initialUriValues

	// URI 0: "" (empty string)
	TRY(createUriTableEntry(uriTable,
									   emptyStr,   // Namespace - empty string
									   TRUE, // Create prefix entry
									   emptyStr,   // Prefix entry - empty string
									   NULL, // No local names
									   0));

	// URI 1: "http://www.w3.org/XML/1998/namespace"
	TRY(createUriTableEntry(uriTable,
									   XML_NAMESPACE,     // URI: "http://www.w3.org/XML/1998/namespace"
									   TRUE,      // Create prefix entry
									   URI_1_PFX, // Prefix: "xml"
									   URI_1_LN,  // Add local names
									   URI_1_LN_SIZE));

	// URI 2: "http://www.w3.org/2001/XMLSchema-instance"
	TRY(createUriTableEntry(uriTable,
									   XML_SCHEMA_INSTANCE,     // URI: "http://www.w3.org/2001/XMLSchema-instance"
									   TRUE,		// Create prefix entry
									   URI_2_PFX, // Prefix: "xsi"
									   URI_2_LN,  // Add local names
									   URI_2_LN_SIZE));

	if(withSchema == TRUE)
	{
		// URI 3: "http://www.w3.org/2001/XMLSchema"
		TRY(createUriTableEntry(uriTable,
										   XML_SCHEMA_NAMESPACE,    // URI: "http://www.w3.org/2001/XMLSchema"
										   FALSE,    // No prefix entry (see http://www.w3.org/TR/exi/#initialPrefixValues) 
										   emptyStr,     // (no prefix)
										   URI_3_LN, // Add local names
										   URI_3_LN_SIZE));
	}

	return EXIP_OK;
}
Esempio n. 25
0
static char* allocFromUTF8(const char* in, size_t len)
{
    if (len > 0) {
        if (len == SIZE_MAX) {
            return NULL;
        }
        SharedBuffer* buf = SharedBuffer::alloc(len+1);
        ALOG_ASSERT(buf, "Unable to allocate shared buffer");
        if (buf) {
            char* str = (char*)buf->data();
            memcpy(str, in, len);
            str[len] = 0;
            return str;
        }
        return NULL;
    }

    return getEmptyString();
}
Esempio n. 26
0
status_t String16::setTo(const String16& other, size_t len, size_t begin)
{
    const size_t N = other.size();
    if (begin >= N) {
        SharedBuffer::bufferFromData(mString)->release();
        mString = getEmptyString();
        return NO_ERROR;
    }
    if ((begin+len) > N) len = N-begin;
    if (begin == 0 && len == N) {
        setTo(other);
        return NO_ERROR;
    }

    if (&other == this) {
        LOG_ALWAYS_FATAL("Not implemented");
    }

    return setTo(other.string()+begin, len);
}
Esempio n. 27
0
void test_Insert(void) {
	STRING_TYPE a = getString("abc");
	STRING_TYPE b = getString("de");

	b = getString("de");
	CU_ASSERT_EQUAL(Insert(&b, 1, a), OK);
	CU_ASSERT_EQUAL(Compare(b, getString("abcde")), 0);

	b = getString("de");
	CU_ASSERT_EQUAL(Insert(&b, 2, a), OK);
	CU_ASSERT_EQUAL(Compare(b, getString("dabce")), 0);

	b = getString("de");
	CU_ASSERT_EQUAL(Insert(&b, 2, getEmptyString()), OK);
	CU_ASSERT_EQUAL(Compare(b, getString("de")), 0);

	b = getString("de");
	CU_ASSERT_EQUAL(Insert(&b, 3, a), OK);
	CU_ASSERT_EQUAL(Compare(b, getString("deabc")), 0);

	CU_ASSERT_EQUAL(Insert(&b, 0, a), ERROR);
}
Esempio n. 28
0
void test_Index(void) {
	STRING_TYPE a = getString("abc");
	STRING_TYPE b = getString("de");
	STRING_TYPE c = getString("abcdeabc");

	CU_ASSERT_EQUAL(Index(c, a, 1), 1);
	CU_ASSERT_EQUAL(Index(c, a, 2), 6);
	CU_ASSERT_EQUAL(Index(c, a, 6), 6);
	CU_ASSERT_EQUAL(Index(c, a, 7), 0);

	CU_ASSERT_EQUAL(Index(c, b, 1), 4);
	CU_ASSERT_EQUAL(Index(c, b, 5), 0);

	CU_ASSERT_EQUAL(Index(c, getEmptyString(), 5), 5);

	STRING_TYPE d = getString("acabaabaabcacaabc");
	STRING_TYPE e = getString("abaabcac");
	CU_ASSERT_EQUAL(Index(d, e, 1), 6);

	STRING_TYPE f = getString("aaabaaaab");
	STRING_TYPE g = getString("aaaab");
	CU_ASSERT_EQUAL(Index(f, g, 1), 5);
}
Esempio n. 29
0
	void String8::clear() {
		SharedBuffer::bufferFromData(mString)->release();
		mString = getEmptyString();
	}
Esempio n. 30
0
	String8::String8()
		: mString(getEmptyString())
	{
	}