⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stafstring.cpp

📁 Software Testing Automation Framework (STAF)的开发代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    STAFStringImplementation &str = **pString;    // Determine the size of the new buffer to allocate by reading the    // string array and determine the combined length of all the strings    // in the array    unsigned int joinByteLen = 0;    unsigned int joinCharLen = 0;    unsigned int i = 0;    for (i = 0; i < arraySize; ++i)    {        if (aStringArray[i] == 0)            continue;                joinByteLen += aStringArray[i]->fByteLen;        joinCharLen += aStringArray[i]->fCharLen;    }        // If joinByteLen is 0, create an empty string    if (joinByteLen == 0)    {        str.pBuffer  = (char *)EMPTY_STRING;        str.fBuffLen = 0;        str.fCharLen = 0;        str.fByteLen = 0;        return kSTAFOk;    }    // Determine size of new buffer to allocate (with extra space)    unsigned int joinBuffLen = getBufferSize(joinByteLen);    // Allocate the buffer to contain all of the strings joined together    char *newbuff = new char[joinBuffLen];    memset(newbuff, 0, joinBuffLen);    // Read string array and copy each string into the buffer        unsigned int currLen = 0;    for (i = 0; i < arraySize; ++i)    {        if ((aStringArray[i] == 0) ||            (aStringArray[i]->pBuffer == EMPTY_STRING))        {            continue;        }                memcpy(newbuff + currLen, aStringArray[i]->pBuffer,               aStringArray[i]->fByteLen);        currLen += aStringArray[i]->fByteLen;    }    str.pBuffer = newbuff;    str.fBuffLen = joinBuffLen;    str.fCharLen = joinCharLen;    str.fByteLen = joinByteLen;    return kSTAFOk;}STAFRC_t STAFStringAssign(STAFString_t aTarget,                          STAFStringConst_t aSource,                          unsigned int *osRC){    // if space allocated in target is not much larger (but    // sufficient to contain source), then just do a memcpy.    // if space allocated in target is much larger or space     // allocated in target is not sufficient, reallocate to    // the size of source and do memcpy.    if ((aTarget->fBuffLen >= aSource->fBuffLen) &&        (aTarget->fBuffLen < 2 * aSource->fBuffLen))     {        memcpy(aTarget->pBuffer, aSource->pBuffer,               aSource->fByteLen);        aTarget->fCharLen = aSource->fCharLen;        aTarget->fByteLen = aSource->fByteLen;    }    else    {        if (aTarget->pBuffer != EMPTY_STRING)            delete[] aTarget->pBuffer;        aTarget->pBuffer = new char[aSource->fBuffLen];        memcpy(aTarget->pBuffer, aSource->pBuffer,               aSource->fByteLen);        aTarget->fCharLen = aSource->fCharLen;        aTarget->fByteLen = aSource->fByteLen;        aTarget->fBuffLen = aSource->fBuffLen;    }    return kSTAFOk;}STAFRC_t STAFStringNumOfWords(STAFStringConst_t aString,                              unsigned int *num,                              unsigned int *osRC){       if (aString == 0) return kSTAFInvalidObject;    if (num == 0) return kSTAFInvalidParm;    char *ptr = aString->pBuffer;    char *lim = aString->pBuffer + aString->fByteLen;    *num = 0;  // default to none    while (ptr < lim)    {        // iterate while blank, when non-blank increment count,        // iterate while non-blank, loop         while (ptr < lim && WHITESPACE(ptr))             ptr = NEXT(ptr);        if (ptr >= lim) break;        (*num)++;        while (ptr < lim && !WHITESPACE(ptr))             ptr = NEXT(ptr);    }    return kSTAFOk;}STAFRC_t STAFStringGetBuffer(STAFStringConst_t aString,                             const char **buffer,                              unsigned int *len,                             unsigned int *osRC){    if (aString == 0) return kSTAFInvalidObject;    if (buffer == 0) return kSTAFInvalidParm;    *buffer = aString->pBuffer;        if (len) *len = aString->fByteLen;    return kSTAFOk;}STAFRC_t STAFStringToLowerCase(STAFString_t aString, unsigned int *osRC){    if (aString == 0) return kSTAFInvalidObject;    // ASCII 0x41 .. 0x5a are upper -> | 0x20 -> lower    char *ptr = aString->pBuffer;    char *lim = aString->pBuffer + aString->fByteLen;    // XXX: may not work on other systems    while (ptr < lim)    {        if (*ptr >= 0x41 && *ptr <= 0x5a)             *ptr |= 0x20;        ptr = NEXT(ptr);    }        return kSTAFOk;}STAFRC_t STAFStringToUpperCase(STAFString_t aString, unsigned int *osRC){    if (aString == 0) return kSTAFInvalidObject;    // ASCII 0x61 .. 0x7a are lower -> & 0xdf -> upper    char *ptr = aString->pBuffer;    char *lim = aString->pBuffer + aString->fByteLen;    // XXX: may not work on other systems    while (ptr < lim)    {        if (*ptr >= 0x61 && *ptr <= 0x7a)             *ptr &= 0xdf;        ptr = NEXT(ptr);    }        return kSTAFOk;}STAFRC_t STAFStringReplace(STAFString_t aString,                            STAFStringConst_t oldString,                           STAFStringConst_t newString,                            unsigned int *osRC){    unsigned int pos  = 0;    unsigned int next = 0;    unsigned int alen = 0;    unsigned int xlen = 0;    unsigned int ylen = 0;    unsigned int clen = 0;    char *astr = 0;    char *xstr = 0;    char *ystr = 0;    char *cstr = 0;    if (aString == 0) return kSTAFInvalidObject;    if (oldString == 0 || newString == 0)        return kSTAFInvalidParm;    astr = aString->pBuffer;    STAFStringLength(oldString, &xlen, ISBYTE, osRC);    ystr = newString->pBuffer;    ylen = newString->fByteLen;    STAFStringFind(aString, oldString, next, ISBYTE, &pos, osRC);    while (pos != 0xffffffff)    {        xstr = astr + pos;        alen = xstr - astr;        cstr = xstr + xlen;        clen = aString->fByteLen - alen - xlen;        next = pos + ylen;        if (alen + ylen + clen <= aString->fByteLen)        {            // replacer ystr is equal or shorter than xstr            memcpy(xstr, ystr, ylen);            memmove(xstr + ylen, cstr, clen);            aString->fByteLen = aString->fByteLen - (xlen - ylen);        }        else        {            // replacer is larger than the buffer            if (alen + ylen + clen >= aString->fBuffLen)            {                char *newbuff = new char[aString->fBuffLen << 1];                if (newbuff == 0)                {                    return kSTAFBaseOSError;                }                memset(newbuff, 0, (aString->fBuffLen << 1));                memcpy(newbuff, aString->pBuffer, aString->fByteLen);                if (aString->pBuffer != EMPTY_STRING)                    delete[] aString->pBuffer;                aString->pBuffer  = newbuff;                aString->fBuffLen = aString->fBuffLen << 1;                astr = aString->pBuffer;                xstr = astr + pos;                cstr = xstr + xlen;            }            memmove(xstr + ylen, cstr, clen);            memcpy(xstr, ystr, ylen);            aString->fByteLen = aString->fByteLen + (ylen - xlen);        }        STAFStringFind(aString, oldString, next, ISBYTE, &pos, osRC);    }    // a bummer, but since we keep this info, we need to recompute    // (not trivial to do while replacing since we allow substring    // replacement, as instead of char replacement)    aString->fCharLen = 0;    astr = (char *)aString->pBuffer;    cstr = (char *)aString->pBuffer + aString->fByteLen;    while (astr < cstr)    {        aString->fCharLen++;        astr = NEXT(astr);    }    return kSTAFOk;}STAFRC_t STAFStringToCurrentCodePage(STAFStringConst_t aString,                                      char **to,                                      unsigned int *len,                                     unsigned int *osRC){    STAFRC_t rc = kSTAFOk;    if (aString == 0) return kSTAFInvalidObject;    if (sConverterPtr == 0)    {        STAFMutexSemLock lock(sConverterSem);        sConverterPtr = new STAFConverter();    }    const unsigned int SIZE = 4096;    const unsigned char *fromPtr = (unsigned char *)aString->pBuffer;    unsigned int fromLen = aString->fByteLen;    unsigned char *toPtr = new unsigned char[SIZE];    unsigned int toLen = SIZE;    std::string result = "";    unsigned int resultLen = 0;    // now let's actually do the conversion    while (fromLen > 0)    {        int rc2 = sConverterPtr->convertFromUTF8(&fromPtr, &fromLen, toPtr,                                                 &toLen);        if (rc2)         {            delete[] toPtr;            if (osRC) *osRC = 0;            return kSTAFConverterError;        }        result += std::string((char *)toPtr, toLen);        resultLen += toLen;        toLen = SIZE;    }    delete[] toPtr;    *to = new char[result.length() + 1];    memcpy(*to, result.data(), result.length());    (*to)[result.length()] = 0;    *len = result.length();    return kSTAFOk;}STAFRC_t STAFStringConcatenate(STAFString_t aString,                                STAFStringConst_t aSource,                               unsigned int *osRC){    if (aString == 0) return kSTAFInvalidObject;    if (aSource == 0) return kSTAFInvalidParm;    char *ptr = 0;    // pre : t = "hello ", s = "world"    // post: t = "hello world", s = "world"    if (aString->fBuffLen > aString->fByteLen + aSource->fByteLen)    {        ptr = aString->pBuffer + aString->fByteLen;        memcpy(ptr, aSource->pBuffer, aSource->fByteLen);        aString->fByteLen += aSource->fByteLen;        aString->fCharLen += aSource->fCharLen;    }    else    {        // Determine size of new buffer to allocate (with extra space)        int concatBufLen = getBufferSize(            aString->fByteLen + aSource->fByteLen);        char *newbuff = new char[concatBufLen];        if (newbuff == 0) return kSTAFBaseOSError;        memset(newbuff, 0, concatBufLen);        memcpy(newbuff, aString->pBuffer, aString->fByteLen);        memcpy(newbuff + aString->fByteLen, aSource->pBuffer,                aSource->fByteLen);        if (aString->pBuffer != EMPTY_STRING)            delete[] aString->pBuffer;        aString->pBuffer = newbuff;        aString->fBuffLen = concatBufLen;        aString->fCharLen += aSource->fCharLen;        aString->fByteLen += aSource->fByteLen;    }    return kSTAFOk;}STAFRC_t STAFStringCountSubStrings(STAFStringConst_t aString,                                   STAFStringConst_t aSubStr,                                   unsigned int *count,                                   unsigned int *osRC){    if ((aString == 0) || (aSubStr == 0)) return kSTAFInvalidObject;    if (count == 0) return kSTAFInvalidParm;    char *ptr = aString->pBuffer;    char *lim = aString->pBuffer + aString->fByteLen;    const char *key = aSubStr->pBuffer;    *count = 0;  // default to none found    while (ptr < lim)    {        // check lead bytes, if equal do comparison        if (*ptr == *key)        {            if (memcmp(key, ptr, aSubStr->fByteLen) == 0)                (*count)++;        }        ptr = NEXT(ptr);    }    return kSTAFOk;}STAFRC_t STAFStringStripCharsOfType(STAFString_t aInOutStr,                                     STAFUTF8CharType_t aType,                                      unsigned int side,                                    unsigned int *osRC){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -