Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Util SP - String.c
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

MSVC_PRAGMA(warning( push, 3 ))
#include <string.h>
MSVC_PRAGMA(warning( pop ))


/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Functions                                                                                                                                                  */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

TgFORCEINLINE P_TgCHAR                               tgCM_UTS_ST_Get_Buffer( PCU_STg2_UTS_ST );
TgFORCEINLINE TgVOID                                 tgCM_UTS_ST_Null_Term( PCU_STg2_UTS_ST, C_TgSIZE );
TgFORCEINLINE TgBOOL                                 tgCM_UTS_ST_Overlap( CPC_TgVOID, C_TgSIZE, CPC_TgVOID, C_TgSIZE );




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCM_UTS_ST_CH_Assign ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_CH_Assign( PC_STg2_UTS_ST psDest, C_TgSIZE uiCount, C_TgCHAR uiChar )
{
    if (TgEXPECT_FALSE( !(0 != uiCount && TgT('\0') != uiChar) ))
    {
        tgCM_UTS_ST_Clear( psDest );
    }
    else
    {
        C_TgSIZE                            uiNew_Reserve = uiCount + 1;
        P_TgCHAR                            pszDest;
    #if defined(TgCOMPILE_WIDE_CHAR)
        TgUINT32                            uiIndex;
    #endif

        if (uiNew_Reserve == psDest->m_uiReserve)
        {
            pszDest = tgCM_UTS_ST_Get_Buffer( psDest );
        }
        else if (uiNew_Reserve <= KTgSTRING_STATIC_BUFFER_SIZE)
        {
            if (psDest->m_uiReserve > KTgSTRING_STATIC_BUFFER_SIZE)
            {
                TgFREE_POOL( tgCM_UTS_ST_Get_Buffer( psDest ) );
                psDest->m_uiReserve = KTgSTRING_STATIC_BUFFER_SIZE;
            };
            pszDest = psDest->m_unData.m_aBuffer;
        }
        else
        {
            pszDest = (P_TgCHAR)TgMALLOC_POOL( uiNew_Reserve * KTgBYTE_PER_CHAR );

            if (TgEXPECT_FALSE( 0 == pszDest ))
            {
                TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_CH_Assign") );
                tgCM_UTS_ST_Clear( psDest );
                return;
            };

            tgCM_UTS_ST_Clear( psDest );
            psDest->m_unData.m_pszDest = pszDest;
            psDest->m_uiReserve = uiNew_Reserve;
        };

    #if defined(TgCOMPILE_WIDE_CHAR)
        for (uiIndex = 0; uiIndex < uiCount; ++uiIndex)
        {
            pszDest[uiIndex] = uiChar;
        };
    #else
        memset( pszDest, uiChar, uiCount );
    #endif
        tgCM_UTS_ST_Null_Term( psDest, uiCount );
    };
}


/* ---- tgCM_UTS_ST_SF_Assign ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_SF_Assign( PC_STg2_UTS_ST psDest, CPC_STg2_UTS_ST psSrc, C_TgSIZE uiSrc_Offset, C_TgSIZE uiSrc_Length )
{
    C_TgSIZE                            uiSize = tgCM_MIN_UXX( uiSrc_Length, psSrc->m_uiBuffer - uiSrc_Offset );

    TgPARAM_CHECK(nullptr != psDest && nullptr != psSrc);

    if (TgEXPECT_FALSE( !(uiSize > 0 && uiSrc_Offset <= psSrc->m_uiBuffer) ))
    {
        tgCM_UTS_ST_Clear( psDest );
    }
    else
    {
        P_TgCHAR                            pszDest = tgCM_UTS_ST_Get_Buffer( psDest );
        CPC_TgCHAR                          pszSrc = tgCM_UTS_ST_Query_String( psSrc );

        if (TgEXPECT_FALSE( tgCM_UTS_ST_Overlap( pszDest, psDest->m_uiBuffer, pszSrc, uiSize ) ))
        {
            C_TgSIZE                            uiOffset = (TgUINT32)(pszSrc - pszDest) + uiSrc_Offset;
            C_TgSIZE                            nuiLeft = tgCM_UTS_ST_Size( psDest );

            tgCM_UTS_SF_Erase( psDest, uiOffset + uiSize, nuiLeft );
            tgCM_UTS_SF_Erase( psDest, 0, uiOffset );
        }
        else
        {
            C_TgSIZE                            uiNew_Reserve = uiSize + 1;

            if (uiNew_Reserve <= KTgSTRING_STATIC_BUFFER_SIZE)
            {
                if (psDest->m_uiReserve > KTgSTRING_STATIC_BUFFER_SIZE)
                {
                    TgFREE_POOL( pszDest );
                    psDest->m_uiReserve = KTgSTRING_STATIC_BUFFER_SIZE;
                    pszDest = psDest->m_unData.m_aBuffer;
                };
            }
            else
            {
                pszDest = (P_TgCHAR)TgMALLOC_POOL( uiNew_Reserve * KTgBYTE_PER_CHAR );

                if (TgEXPECT_FALSE( 0 == pszDest ))
                {
                    TgERROR_MSGF( uiSize > 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_SF_Assign") );
                    tgCM_UTS_ST_Clear( psDest );
                    return;
                };

                tgCM_UTS_ST_Clear( psDest );
                psDest->m_unData.m_pszDest = pszDest;
                psDest->m_uiReserve = uiNew_Reserve;
            };

            memcpy( pszDest, pszSrc, uiSize * KTgBYTE_PER_CHAR );
            tgCM_UTS_ST_Null_Term( psDest, uiSize );
        };
    };
}


/* ---- tgCM_UTS_ST_CH_Append ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_CH_Append( PC_STg2_UTS_ST psDest, C_TgSIZE uiCount, C_TgCHAR uiChar )
{
    TgPARAM_CHECK(nullptr != psDest);

    if (TgEXPECT_TRUE( 0 != uiCount && TgT('\0') != uiChar ))
    {
        C_TgSIZE                            uiNew_Size = psDest->m_uiBuffer + uiCount;
        P_TgCHAR                            pszDest;
    #if defined(TgCOMPILE_WIDE_CHAR)
        TgUINT32                            uiIndex;
    #endif

        tgCM_UTS_ST_Reserve( psDest, uiNew_Size );

        pszDest = tgCM_UTS_ST_Get_Buffer( psDest );
        TgERROR(nullptr != pszDest);

    #if defined(TgCOMPILE_WIDE_CHAR)
        for (uiIndex = 0; uiIndex < uiCount; ++uiIndex)
        {
            pszDest[psDest->m_uiBuffer + uiIndex] = uiChar;
        };
    #else
        memset( pszDest + psDest->m_uiBuffer, uiChar, uiCount );
    #endif

        tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
    };
}


/* ---- tgCM_UTS_ST_SF_Append ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_SF_Append( PC_STg2_UTS_ST psDest, CPC_STg2_UTS_ST psSrc, C_TgSIZE uiSrc_Offset, C_TgSIZE uiSrc_Length )
{
    C_TgSIZE                            uiSize = tgCM_MIN_UXX( uiSrc_Length, psSrc->m_uiBuffer - uiSrc_Offset );
    C_TgSIZE                            uiNew_Size = psDest->m_uiBuffer + uiSize;
    P_TgCHAR                            pszDest;

    TgPARAM_CHECK(nullptr != psDest && nullptr != psSrc);

    if (TgEXPECT_FALSE( !(uiSize > 0 && uiSrc_Offset <= psSrc->m_uiBuffer) ))
    {
    }
    else if (psDest->m_uiReserve - psDest->m_uiBuffer > uiSize)
    {
        TgVERIFY( 0 != (pszDest = tgCM_UTS_ST_Get_Buffer( psDest )) );
        memcpy( pszDest + psDest->m_uiBuffer, tgCM_UTS_ST_Query_String( psSrc ) + uiSrc_Offset, uiSize * KTgBYTE_PER_CHAR );
        tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
    }
    else
    {
        PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( (uiNew_Size + 1) * KTgBYTE_PER_CHAR );

        if (TgEXPECT_FALSE( 0 == pszTemp ))
        {
            TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_SF_Append") );
            return;
        };

        TgVERIFY( 0 != (pszDest = tgCM_UTS_ST_Get_Buffer( psDest )) );
        TgPARAM_CHECK(nullptr != pszDest);

        memcpy( pszTemp, pszDest, psDest->m_uiBuffer * KTgBYTE_PER_CHAR );
        memcpy( pszTemp + psDest->m_uiBuffer, tgCM_UTS_ST_Query_String( psSrc ) + uiSrc_Offset, uiSize * KTgBYTE_PER_CHAR );

        tgCM_UTS_ST_Clear( psDest );
        psDest->m_unData.m_pszDest = pszTemp;
        psDest->m_uiReserve = uiNew_Size + 1;
        tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
    };
}


/* ---- tgCM_UTS_ST_CH_Insert ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_CH_Insert( PC_STg2_UTS_ST psDest, C_TgSIZE uiIdx, C_TgSIZE uiCount, C_TgCHAR uiChar )
{
    C_TgSIZE                            uiNew_Size = psDest->m_uiBuffer + uiCount;
    C_TgSIZE                            uiNew_Reserve = tgCM_MAX_UXX( psDest->m_uiReserve, uiNew_Size + 1 );
    P_TgCHAR                            pszDest = tgCM_UTS_ST_Get_Buffer( psDest );
#if defined(TgCOMPILE_WIDE_CHAR)
    TgUINT32                            uiIndex;
#endif

    TgPARAM_CHECK(nullptr != psDest);

    if (TgEXPECT_FALSE( !(uiCount > 0 && uiIdx <= psDest->m_uiBuffer) ))
    {
    }
    else if (TgEXPECT_FALSE( TgT('\0') == uiChar ))
    {
        tgCM_UTS_SF_Erase( psDest, uiIdx, psDest->m_uiBuffer );
    }
    else if (uiNew_Reserve == psDest->m_uiReserve || uiNew_Reserve <= KTgSTRING_STATIC_BUFFER_SIZE)
    {
        pszDest += uiIdx;

        memmove( pszDest + uiCount, pszDest, (psDest->m_uiBuffer - uiIdx) * KTgBYTE_PER_CHAR );

    #if defined(TgCOMPILE_WIDE_CHAR)
        for (uiIndex = 0; uiIndex < uiCount; ++uiIndex)
        {
            pszDest[uiIndex] = uiChar;
        };
    #else
        memset( pszDest, uiChar, uiCount );
    #endif

        tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
    }
    else
    {
        PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( uiNew_Reserve * KTgBYTE_PER_CHAR );

        if (TgEXPECT_FALSE( 0 == pszTemp ))
        {
            TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_CH_Insert") );
            return;
        };

        memcpy( pszTemp, pszDest, uiIdx * KTgBYTE_PER_CHAR );

    #if defined(TgCOMPILE_WIDE_CHAR)
        for (uiIndex = 0; uiIndex < uiCount; ++uiIndex)
        {
            pszTemp[uiIdx + uiIndex] = uiChar;
        };
    #else
        memset( pszTemp + uiIdx, uiChar, uiCount );
    #endif

        memcpy( pszTemp + uiIdx + uiCount, pszDest + uiIdx, (psDest->m_uiBuffer - uiIdx) * KTgBYTE_PER_CHAR );

        tgCM_UTS_ST_Clear( psDest );
        psDest->m_unData.m_pszDest = pszTemp;
        psDest->m_uiReserve = uiNew_Reserve;
        tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
    };
}


/* ---- tgCM_UTS_ST_SF_Insert ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_SF_Insert( PC_STg2_UTS_ST psDest, C_TgSIZE uiIdx, CPC_STg2_UTS_ST psSrc, C_TgSIZE uiSrc_Offset, C_TgSIZE uiSrc_Length )
{
    C_TgSIZE                            uiSize = tgCM_MIN_UXX( uiSrc_Length, psSrc->m_uiBuffer - uiSrc_Offset );
    C_TgSIZE                            uiNew_Size = psDest->m_uiBuffer + uiSize;
    C_TgSIZE                            uiNew_Reserve = tgCM_MAX_UXX( psDest->m_uiReserve, uiNew_Size + 1 );
    PC_TgCHAR                           pszDest = tgCM_UTS_ST_Get_Buffer( psDest );
    CP_TgCHAR                           pszSrc = tgCM_UTS_ST_Query_String( psSrc ) + uiSrc_Offset;

    TgPARAM_CHECK(nullptr != psDest && nullptr != psSrc);

    if (TgEXPECT_FALSE( !(uiSize > 0 && uiSrc_Offset <= psSrc->m_uiBuffer && uiIdx <= psDest->m_uiBuffer) ))
    {
    }
    else if (uiNew_Reserve == psDest->m_uiReserve || uiNew_Reserve <= KTgSTRING_STATIC_BUFFER_SIZE)
    {
        if (TgEXPECT_FALSE( tgCM_UTS_ST_Overlap( pszDest + uiIdx, psDest->m_uiBuffer - uiIdx, pszSrc, uiSize ) ))
        {
            PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( uiSize * KTgBYTE_PER_CHAR );

            if (TgEXPECT_FALSE( 0 == pszTemp ))
            {
                TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_SF_Insert") );
                return;
            };

            memcpy( pszTemp, pszSrc, uiSize * KTgBYTE_PER_CHAR );

            memmove( pszDest + uiIdx + uiSize, pszDest + uiIdx, (psDest->m_uiBuffer - uiIdx) * KTgBYTE_PER_CHAR );
            memcpy( pszDest + uiIdx, pszTemp, uiSize * KTgBYTE_PER_CHAR );
            tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );

            TgFREE_POOL( pszTemp );
        }
        else
        {
            memmove( pszDest + uiIdx + uiSize, pszDest + uiIdx, (psDest->m_uiBuffer - uiIdx) * KTgBYTE_PER_CHAR );
            memcpy( pszDest + uiIdx, pszSrc, uiSize * KTgBYTE_PER_CHAR );
            tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
        };
    }
    else
    {
        PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( uiNew_Reserve * KTgBYTE_PER_CHAR );

        if (TgEXPECT_FALSE( 0 == pszTemp ))
        {
            TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_SF_Insert") );
            return;
        };

        memcpy( pszTemp, pszDest, uiIdx * KTgBYTE_PER_CHAR );
        memcpy( pszTemp + uiIdx, pszSrc, uiSize * KTgBYTE_PER_CHAR );
        memcpy( pszTemp + uiIdx + uiSize, pszDest + uiIdx, (psDest->m_uiBuffer - uiIdx) * KTgBYTE_PER_CHAR );

        tgCM_UTS_ST_Clear( psDest );
        psDest->m_unData.m_pszDest = pszTemp;
        psDest->m_uiReserve = uiNew_Reserve;
        tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
    };
}


/* ---- tgCM_UTS_ST_CH_Replace ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_CH_Replace( PC_STg2_UTS_ST psDest, C_TgSIZE uiStart, C_TgSIZE uiEnd, C_TgSIZE uiCount, C_TgCHAR uiChar )
{
    TgPARAM_CHECK(nullptr != psDest);

    if (TgEXPECT_FALSE( !(uiStart <= psDest->m_uiBuffer && uiEnd <= psDest->m_uiBuffer) ))
    {
    }
    else if (TgEXPECT_FALSE( 0 == uiCount ))
    {
        tgCM_UTS_SF_Erase( psDest, uiStart, uiEnd - uiStart );
    }
    else if (TgEXPECT_FALSE( TgT('\0') == uiChar ))
    {
        tgCM_UTS_SF_Erase( psDest, uiStart, ~0u );
    }
    else
    {
        C_TgSIZE                            uiNew_Size = psDest->m_uiBuffer - (uiEnd - uiStart) + uiCount;
        C_TgSIZE                            uiNew_Reserve = tgCM_MAX_UXX( psDest->m_uiReserve, uiNew_Size + 1 );
        P_TgCHAR                            pszDest = tgCM_UTS_ST_Get_Buffer( psDest );
    #if defined(TgCOMPILE_WIDE_CHAR)
        TgUINT32                            uiIndex;
    #endif

        if (uiNew_Reserve == psDest->m_uiReserve || uiNew_Reserve <= KTgSTRING_STATIC_BUFFER_SIZE)
        {
            memmove( pszDest + uiStart + uiCount, pszDest + uiEnd, (psDest->m_uiBuffer - uiEnd) * KTgBYTE_PER_CHAR );

        #if defined(TgCOMPILE_WIDE_CHAR)
            for (uiIndex = 0; uiIndex < uiCount; ++uiIndex)
            {
                pszDest[uiStart + uiIndex] = uiChar;
            };
        #else
            memset( pszDest + uiStart, uiChar, uiCount );
        #endif

            tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
        }
        else
        {
            PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( uiNew_Reserve * KTgBYTE_PER_CHAR );

            if (TgEXPECT_FALSE( 0 == pszTemp ))
            {
                TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_SF_Insert") );
                return;
            };

            memcpy( pszTemp, pszDest, uiStart * KTgBYTE_PER_CHAR );

        #if defined(TgCOMPILE_WIDE_CHAR)
            for (uiIndex = 0; uiIndex < uiCount; ++uiIndex)
            {
                pszTemp[uiStart + uiIndex] = uiChar;
            };
        #else
            memset( pszTemp + uiStart, uiChar, uiCount );
        #endif

            memcpy( pszTemp + uiStart + uiCount, pszDest + uiEnd, (psDest->m_uiBuffer - uiEnd) * KTgBYTE_PER_CHAR );

            tgCM_UTS_ST_Clear( psDest );
            psDest->m_unData.m_pszDest = pszTemp;
            psDest->m_uiReserve = uiNew_Reserve;
            tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
        };
    };
}


/* ---- tgCM_UTS_ST_SF_Replace ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_SF_Replace( PC_STg2_UTS_ST psDest, C_TgSIZE uiDest_Start, C_TgSIZE uiDest_End, CPC_STg2_UTS_ST psSrc, C_TgSIZE uiSrc_Offset, C_TgSIZE uiSrc_Length )
{
    C_TgSIZE                            uiSrc_Size = tgCM_MIN_UXX( uiSrc_Length, psSrc->m_uiBuffer - uiSrc_Offset );
    C_TgSIZE                            uiDest_Size = uiDest_End - uiDest_Start;
    C_TgSIZE                            uiNew_Size = psDest->m_uiBuffer - uiDest_Size + uiSrc_Size;
    C_TgSIZE                            uiNew_Reserve = tgCM_MAX_UXX( psDest->m_uiReserve, uiNew_Size + 1 );
    PC_TgCHAR                           pszDest = tgCM_UTS_ST_Get_Buffer( psDest );
    CP_TgCHAR                           pszSrc = tgCM_UTS_ST_Query_String( psSrc ) + uiSrc_Offset;

    TgPARAM_CHECK(nullptr != psDest && nullptr != psSrc);

    if (TgEXPECT_FALSE( !(uiSrc_Offset <= psSrc->m_uiBuffer && uiDest_Start <= psDest->m_uiBuffer && uiDest_End <= psDest->m_uiBuffer) ))
    {
    }
    else if (TgEXPECT_FALSE( !(uiSrc_Size > 0) ))
    {
        tgCM_UTS_SF_Erase( psDest, uiDest_Start, uiDest_Size );
    }
    else if (uiNew_Reserve == psDest->m_uiReserve || uiNew_Reserve <= KTgSTRING_STATIC_BUFFER_SIZE)
    {
        PC_TgCHAR                           pszDest_Start = pszDest + uiDest_Start;
        PC_TgCHAR                           pszDest_End = pszDest + uiDest_End;

        if (TgEXPECT_FALSE( tgCM_UTS_ST_Overlap( pszDest, psDest->m_uiBuffer - uiDest_Start, pszSrc, uiSrc_Size ) ))
        {
            PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( uiSrc_Size * KTgBYTE_PER_CHAR );

            if (TgEXPECT_FALSE( 0 == pszTemp ))
            {
                TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_SF_Replace") );
                return;
            };

            memcpy( pszTemp, pszSrc, uiSrc_Size * KTgBYTE_PER_CHAR );

            memmove( pszDest_Start + uiSrc_Size, pszDest_End, (psDest->m_uiBuffer - uiDest_End) * KTgBYTE_PER_CHAR );
            memcpy( pszDest_Start, pszTemp, uiSrc_Size * KTgBYTE_PER_CHAR );
            tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );

            TgFREE_POOL( pszTemp );
        }
        else
        {
            memmove( pszDest_Start + uiSrc_Size, pszDest_End, (psDest->m_uiBuffer - uiDest_End) * KTgBYTE_PER_CHAR );
            memcpy( pszDest_Start, pszSrc, uiSrc_Size * KTgBYTE_PER_CHAR );
            tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
        };
    }
    else
    {
        PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( uiNew_Reserve * KTgBYTE_PER_CHAR );

        if (TgEXPECT_FALSE( 0 == pszTemp ))
        {
            TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_SF_Replace") );
            return;
        };

        memcpy( pszTemp, pszDest, uiDest_Start * KTgBYTE_PER_CHAR );
        memcpy( pszTemp + uiDest_Start, pszSrc, uiSrc_Size * KTgBYTE_PER_CHAR );
        memcpy( pszTemp + uiDest_Start + uiSrc_Size, pszDest + uiDest_End, (psDest->m_uiBuffer - uiDest_End) * KTgBYTE_PER_CHAR );

        tgCM_UTS_ST_Clear( psDest );
        psDest->m_unData.m_pszDest = pszTemp;
        psDest->m_uiReserve = uiNew_Reserve;
        tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
    };
}


/* ---- tgCM_UTS_SF_Erase ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* Erase elements [uiOffset, uiOffset + uiLength) */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_SF_Erase( PC_STg2_UTS_ST psDest, C_TgSIZE uiOffset, C_TgSIZE uiLength )
{
    C_TgSIZE                            uiSize = tgCM_MIN_UXX( uiLength, psDest->m_uiBuffer - uiOffset );
    PC_TgCHAR                           pszDest = tgCM_UTS_ST_Get_Buffer( psDest ) + uiOffset;

    TgPARAM_CHECK(nullptr != psDest);

    if (TgEXPECT_FALSE( !(uiOffset <= psDest->m_uiBuffer && uiSize > 0) ))
    {
    }
    else
    {
        if (psDest->m_uiBuffer > uiOffset + uiSize)
        {
            memmove( pszDest, pszDest + uiSize, (psDest->m_uiBuffer - uiSize - uiOffset) * KTgBYTE_PER_CHAR );
        };

        tgCM_UTS_ST_Null_Term( psDest, psDest->m_uiBuffer - uiSize );
    };
}


/* ---- tgCM_UTS_ST_Resize ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgCM_UTS_ST_Resize( PC_STg2_UTS_ST psDest, C_TgSIZE uiNew_Size, C_TgCHAR uiChar )
{
    TgPARAM_CHECK(nullptr != psDest);

    if (TgEXPECT_FALSE( uiNew_Size > psDest->m_uiBuffer && TgT('\0') != uiChar ))
    {
        tgCM_UTS_ST_CH_Append( psDest, uiNew_Size - psDest->m_uiBuffer, uiChar );
    }
    else
    {
        TgSIZE                              uiIndex = psDest->m_uiBuffer;
        P_TgCHAR                            pszDest;

        if (uiNew_Size + 1 < KTgSTRING_STATIC_BUFFER_SIZE)
        {
            if (psDest->m_uiReserve > KTgSTRING_STATIC_BUFFER_SIZE)
            {
                pszDest = tgCM_UTS_ST_Get_Buffer( psDest );

                memcpy( psDest->m_unData.m_aBuffer, pszDest, tgCM_MIN_UXX( uiNew_Size, psDest->m_uiBuffer ) * KTgBYTE_PER_CHAR );
                TgFREE_POOL( pszDest );
                psDest->m_uiReserve = KTgSTRING_STATIC_BUFFER_SIZE;
            };

            pszDest = psDest->m_unData.m_aBuffer;
        }
        else
        {
            PCU_TgCHAR                          pszTemp = (P_TgCHAR)TgMALLOC_POOL( (uiNew_Size + 1) * KTgBYTE_PER_CHAR );

            if (TgEXPECT_FALSE( 0 == pszTemp ))
            {
                TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Out of Memory.\n"), TgT("Common"), TgT("CM_UTS_ST_Resize") );
                return;
            };

            pszDest = tgCM_UTS_ST_Get_Buffer( psDest );

            if (TgEXPECT_TRUE( psDest->m_uiBuffer > 0 ))
            {
                memcpy( pszTemp, pszDest, tgCM_MIN_UXX( uiNew_Size, psDest->m_uiBuffer ) * KTgBYTE_PER_CHAR );
            };

            tgCM_UTS_ST_Clear( psDest );
            psDest->m_unData.m_pszDest = pszTemp;
            psDest->m_uiReserve = uiNew_Size + 1;

            pszDest = pszTemp;
        };

        if (TgT('\0') != uiChar && uiNew_Size > uiIndex)
        {
        #if defined(TgCOMPILE_WIDE_CHAR)
            for (; uiIndex < uiNew_Size; ++uiIndex)
            {
                pszDest[uiIndex] = uiChar;
            };
        #else
            memset( pszDest + uiIndex, uiChar, uiNew_Size - uiIndex );
        #endif

            tgCM_UTS_ST_Null_Term( psDest, uiNew_Size );
        }
        else
        {
            tgCM_UTS_ST_Null_Term( psDest, tgCM_MIN_UXX( uiIndex, uiNew_Size ) );
        };
    };
}


/* ---- tgCM_UTS_SO_SF_Find_String -------------------------------------------------------------------------------------------------------------------------------------- */
/* Look for [pszRight, pszRight + uiRLength) beginning at or after uiOffset */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgCM_UTS_SO_SF_Find_String( CPC_STg2_UTS_ST psLeft, C_TgSIZE uiLOffset, CPC_STg2_UTS_ST psRight, C_TgSIZE uiROffset, C_TgSIZE uiRLength )
{
    C_TgSIZE                            uiRSize = tgCM_MIN_UXX( uiRLength, psRight->m_uiBuffer - uiROffset );
    C_TgSIZE                            uiLSize = psLeft->m_uiBuffer - uiLOffset;

    if (TgEXPECT_FALSE(!(uiLSize > 0 && uiRSize > 0 && uiLOffset + uiRSize <= psLeft->m_uiBuffer && uiROffset <= psRight->m_uiBuffer) ))
    {
        return (KTgMAX_SIZE);
    }
    else
    {
        CPC_TgCHAR                          pszRight = tgCM_UTS_ST_Query_String( psRight ) + uiROffset;
        CPC_TgCHAR                          pszLeft = tgCM_UTS_ST_Query_String( psLeft );
        CPC_TgCHAR                          pszLast = pszLeft + (psLeft->m_uiBuffer - 1) - (uiRSize - 1);
        CP_TgCHAR                           pszCurrent = pszLeft + uiLOffset;

        while (1)
        {
            for (; *pszCurrent != *pszRight && pszCurrent <= pszLast; ++pszCurrent);

            if (TgEXPECT_FALSE( !(pszCurrent <= pszLast) ))
            {
                break;
            };

            if (0 == tgSZ_CompareN( pszCurrent, pszRight, uiRSize ))
            {
                return ((TgSIZE)(pszCurrent - pszLeft)); /* Found a match */
            };

            ++pszCurrent;
        };

        return (KTgMAX_SIZE);
    };
}


/* ---- tgCM_UTS_SO_SF_RFind_String ------------------------------------------------------------------------------------------------------------------------------------- */
/* Look for [pszRight, pszRight + uiCount) beginning at or after uiOffset */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgCM_UTS_SO_SF_RFind_String( CPC_STg2_UTS_ST psLeft, C_TgSIZE uiLOffset, CPC_STg2_UTS_ST psRight, C_TgSIZE uiROffset, C_TgSIZE uiRLength )
{
    C_TgSIZE                            uiSize = tgCM_MIN_UXX( uiRLength, psRight->m_uiBuffer - uiROffset );

    if (TgEXPECT_FALSE( !(uiSize > 0 && uiROffset <= psRight->m_uiBuffer && uiLOffset + uiSize <= psLeft->m_uiBuffer) ))
    {
        return (KTgMAX_SIZE);
    }
    else
    {
        CPC_TgCHAR                          pszRight = tgCM_UTS_ST_Query_String( psRight ) + uiROffset;
        CPC_TgCHAR                          pszLeft = tgCM_UTS_ST_Query_String( psLeft );
        CP_TgCHAR                           pszCurrent = pszLeft - uiLOffset + (psLeft->m_uiBuffer - 1) - (uiSize - 1);

        while (1)
        {
            for (; *pszCurrent != *pszRight && pszCurrent >= pszLeft; --pszCurrent);

            if (TgEXPECT_FALSE( !(pszCurrent >= pszLeft) ))
            {
                break;
            };

            if (0 == tgSZ_CompareN( pszCurrent, pszRight, uiSize ))
            {
                return ((TgSIZE)(pszCurrent - pszLeft)); /* Found a match */
            };

            --pszCurrent;
        };

        return (KTgMAX_SIZE);
    };
}




/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  File Local Functions                                                                                                                                                  */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCM_UTS_ST_Get_Buffer ------------------------------------------------------------------------------------------------------------------------------------------ */
/* Determine current pointer to buffer for non mutable string */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFORCEINLINE P_TgCHAR tgCM_UTS_ST_Get_Buffer( PCU_STg2_UTS_ST psDest )
{
    return (KTgSTRING_STATIC_BUFFER_SIZE < psDest->m_uiReserve ? psDest->m_unData.m_pszDest : psDest->m_unData.m_aBuffer);
}


/* ---- tgCM_UTS_ST_Null_Term ------------------------------------------------------------------------------------------------------------------------------------------- */
/* Set new length and null terminator */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFORCEINLINE TgVOID tgCM_UTS_ST_Null_Term( PCU_STg2_UTS_ST psDest, C_TgSIZE uiNewSize )
{
    tgCM_UTS_ST_Get_Buffer( psDest )[psDest->m_uiBuffer = uiNewSize] = TgT('\0');
}


/* ---- tgCM_UTS_ST_Overlap --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgFORCEINLINE TgBOOL tgCM_UTS_ST_Overlap( CPC_TgVOID pData0, C_TgSIZE uiRng0, CPC_TgVOID pData1, C_TgSIZE uiRng1 )
{
    return (!(((TgUINTPTR)pData0 >= (TgUINTPTR)pData1 + uiRng1*KTgBYTE_PER_CHAR) || ((TgUINTPTR)pData0 + uiRng0*KTgBYTE_PER_CHAR < (TgUINTPTR)pData1)));
}