Changeset 91789 in vbox for trunk/src/VBox/Runtime/common/string
- Timestamp:
- Oct 17, 2021 6:16:11 PM (3 years ago)
- Location:
- trunk/src/VBox/Runtime/common/string
- Files:
-
- 1 added
- 3 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/string/strformat.cpp
r90821 r91789 833 833 RT_EXPORT_SYMBOL(RTStrFormatV); 834 834 835 836 /**837 * Partial implementation of a printf like formatter.838 * It doesn't do everything correct, and there is no floating point support.839 * However, it supports custom formats by the means of a format callback.840 *841 * @returns number of bytes formatted.842 * @param pfnOutput Output worker.843 * Called in two ways. Normally with a string an it's length.844 * For termination, it's called with NULL for string, 0 for length.845 * @param pvArgOutput Argument to the output worker.846 * @param pfnFormat Custom format worker.847 * @param pvArgFormat Argument to the format worker.848 * @param pszFormat Format string.849 * @param ... Argument list.850 */851 RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...)852 {853 size_t cch;854 va_list args;855 va_start(args, pszFormat);856 cch = RTStrFormatV(pfnOutput, pvArgOutput, pfnFormat, pvArgFormat, pszFormat, args);857 va_end(args);858 return cch;859 }860 RT_EXPORT_SYMBOL(RTStrFormat);861 -
trunk/src/VBox/Runtime/common/string/strprintf-ellipsis.cpp
r91787 r91789 1 1 /* $Id$ */ 2 2 /** @file 3 * IPRT - String Formatters .3 * IPRT - String Formatters, Ellipsis Functions. 4 4 */ 5 5 … … 32 32 #include "internal/iprt.h" 33 33 34 #include <iprt/assert.h>35 36 37 /*********************************************************************************************************************************38 * Structures and Typedefs *39 *********************************************************************************************************************************/40 /** strbufoutput() argument structure. */41 typedef struct STRBUFARG42 {43 /** Pointer to current buffer position. */44 char *psz;45 /** Number of bytes left in the buffer - not including the trailing zero. */46 size_t cch;47 } STRBUFARG;48 /** Pointer to a strbufoutput() argument structure. */49 typedef STRBUFARG *PSTRBUFARG;50 51 52 /*********************************************************************************************************************************53 * Internal Functions *54 *********************************************************************************************************************************/55 static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars);56 57 58 /**59 * Output callback.60 *61 * @returns number of bytes written.62 * @param pvArg Pointer to a STRBUFARG structure.63 * @param pachChars Pointer to an array of utf-8 characters.64 * @param cbChars Number of bytes in the character array pointed to by pachChars.65 */66 static DECLCALLBACK(size_t) strbufoutput(void *pvArg, const char *pachChars, size_t cbChars)67 {68 PSTRBUFARG pArg = (PSTRBUFARG)pvArg;69 char *pszCur = pArg->psz; /* We actually have to spell this out for VS2010, or it will load for each case. */70 71 cbChars = RT_MIN(pArg->cch, cbChars);72 if (cbChars)73 {74 pArg->cch -= cbChars;75 76 /* Note! For VS2010/64 we need at least 7 case statements before it generates a jump table. */77 switch (cbChars)78 {79 default:80 memcpy(pszCur, pachChars, cbChars);81 break;82 case 8: pszCur[7] = pachChars[7]; RT_FALL_THRU();83 case 7: pszCur[6] = pachChars[6]; RT_FALL_THRU();84 case 6: pszCur[5] = pachChars[5]; RT_FALL_THRU();85 case 5: pszCur[4] = pachChars[4]; RT_FALL_THRU();86 case 4: pszCur[3] = pachChars[3]; RT_FALL_THRU();87 case 3: pszCur[2] = pachChars[2]; RT_FALL_THRU();88 case 2: pszCur[1] = pachChars[1]; RT_FALL_THRU();89 case 1: pszCur[0] = pachChars[0]; RT_FALL_THRU();90 case 0:91 break;92 }93 pArg->psz = pszCur += cbChars;94 }95 *pszCur = '\0';96 97 return cbChars;98 }99 100 101 RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)102 {103 STRBUFARG Arg;104 AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);105 Arg.psz = pszBuffer;106 Arg.cch = cchBuffer - 1;107 return RTStrFormatV(strbufoutput, &Arg, NULL, NULL, pszFormat, args);108 }109 RT_EXPORT_SYMBOL(RTStrPrintfV);110 111 34 112 35 RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) … … 122 45 123 46 124 RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)125 {126 STRBUFARG Arg;127 AssertMsgReturn(cchBuffer, ("Excellent idea! Format a string with no space for the output!\n"), 0);128 Arg.psz = pszBuffer;129 Arg.cch = cchBuffer - 1;130 return RTStrFormatV(strbufoutput, &Arg, pfnFormat, pvArg, pszFormat, args);131 }132 RT_EXPORT_SYMBOL(RTStrPrintfExV);133 134 135 136 47 RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) 137 48 { -
trunk/src/VBox/Runtime/common/string/strprintf.cpp
r91787 r91789 110 110 111 111 112 RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)113 {114 size_t cbRet;115 va_list va;116 va_start(va, pszFormat);117 cbRet = RTStrPrintfV(pszBuffer, cchBuffer, pszFormat, va);118 va_end(va);119 return cbRet;120 }121 RT_EXPORT_SYMBOL(RTStrPrintf);122 123 124 112 RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) 125 113 { … … 132 120 RT_EXPORT_SYMBOL(RTStrPrintfExV); 133 121 134 135 136 RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)137 {138 va_list args;139 size_t cbRet;140 va_start(args, pszFormat);141 cbRet = RTStrPrintfExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);142 va_end(args);143 return cbRet;144 }145 RT_EXPORT_SYMBOL(RTStrPrintfEx);146 -
trunk/src/VBox/Runtime/common/string/strprintf2-ellipsis.cpp
r91788 r91789 1 1 /* $Id$ */ 2 2 /** @file 3 * IPRT - String Formatters, alternative .3 * IPRT - String Formatters, alternative, ellipsis. 4 4 */ 5 5 … … 32 32 #include "internal/iprt.h" 33 33 34 #include <iprt/assert.h>35 36 37 /*********************************************************************************************************************************38 * Structures and Typedefs *39 *********************************************************************************************************************************/40 /** rtStrPrintf2Output() argument structure. */41 typedef struct STRPRINTF2OUTPUTARGS42 {43 /** Pointer to current buffer position. */44 char *pszCur;45 /** Number of bytes left in the buffer (including the trailing zero). */46 size_t cbLeft;47 /** Set if we overflowed. */48 bool fOverflowed;49 } STRPRINTF2OUTPUTARGS;50 /** Pointer to a rtStrPrintf2Output() argument structure. */51 typedef STRPRINTF2OUTPUTARGS *PSTRPRINTF2OUTPUTARGS;52 53 54 /**55 * Output callback.56 *57 * @returns cbChars58 *59 * @param pvArg Pointer to a STRBUFARG structure.60 * @param pachChars Pointer to an array of utf-8 characters.61 * @param cbChars Number of bytes in the character array pointed to by pachChars.62 */63 static DECLCALLBACK(size_t) rtStrPrintf2Output(void *pvArg, const char *pachChars, size_t cbChars)64 {65 PSTRPRINTF2OUTPUTARGS pArgs = (PSTRPRINTF2OUTPUTARGS)pvArg;66 char *pszCur = pArgs->pszCur; /* We actually have to spell this out for VS2010, or it will load it for each case. */67 68 if (cbChars < pArgs->cbLeft)69 {70 pArgs->cbLeft -= cbChars;71 72 /* Note! For VS2010/64 we need at least 7 case statements before it generates a jump table. */73 switch (cbChars)74 {75 default:76 memcpy(pszCur, pachChars, cbChars);77 break;78 case 8: pszCur[7] = pachChars[7]; RT_FALL_THRU();79 case 7: pszCur[6] = pachChars[6]; RT_FALL_THRU();80 case 6: pszCur[5] = pachChars[5]; RT_FALL_THRU();81 case 5: pszCur[4] = pachChars[4]; RT_FALL_THRU();82 case 4: pszCur[3] = pachChars[3]; RT_FALL_THRU();83 case 3: pszCur[2] = pachChars[2]; RT_FALL_THRU();84 case 2: pszCur[1] = pachChars[1]; RT_FALL_THRU();85 case 1: pszCur[0] = pachChars[0]; RT_FALL_THRU();86 case 0:87 break;88 }89 pArgs->pszCur = pszCur += cbChars;90 *pszCur = '\0';91 }92 else93 {94 size_t cbLeft = pArgs->cbLeft;95 if (cbLeft-- > 1)96 {97 memcpy(pszCur, pachChars, cbLeft);98 pArgs->pszCur = pszCur += cbLeft;99 *pszCur = '\0';100 pArgs->cbLeft = 1;101 }102 pArgs->fOverflowed = true;103 }104 105 return cbChars;106 }107 108 RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args)109 {110 STRPRINTF2OUTPUTARGS Args;111 size_t cchRet;112 AssertMsg(cchBuffer > 0, ("Excellent idea! Format a string with no space for the output!\n"));113 114 Args.pszCur = pszBuffer;115 Args.cbLeft = cchBuffer;116 Args.fOverflowed = false;117 118 cchRet = RTStrFormatV(rtStrPrintf2Output, &Args, NULL, NULL, pszFormat, args);119 120 return !Args.fOverflowed ? (ssize_t)cchRet : -(ssize_t)cchRet - 1;121 }122 RT_EXPORT_SYMBOL(RTStrPrintf2V);123 124 125 34 126 35 RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) … … 136 45 137 46 138 RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,139 const char *pszFormat, va_list args)140 {141 STRPRINTF2OUTPUTARGS Args;142 size_t cchRet;143 AssertMsg(cchBuffer > 0, ("Excellent idea! Format a string with no space for the output!\n"));144 145 Args.pszCur = pszBuffer;146 Args.cbLeft = cchBuffer;147 Args.fOverflowed = false;148 cchRet = RTStrFormatV(rtStrPrintf2Output, &Args, pfnFormat, pvArg, pszFormat, args);149 return !Args.fOverflowed ? (ssize_t)cchRet : -(ssize_t)cchRet - 1;150 }151 RT_EXPORT_SYMBOL(RTStrPrintf2ExV);152 153 154 47 RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) 155 48 { -
trunk/src/VBox/Runtime/common/string/strprintf2.cpp
r91788 r91789 106 106 } 107 107 108 108 109 RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) 109 110 { … … 123 124 124 125 125 126 RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)127 {128 va_list va;129 ssize_t cbRet;130 va_start(va, pszFormat);131 cbRet = RTStrPrintf2V(pszBuffer, cchBuffer, pszFormat, va);132 va_end(va);133 return cbRet;134 }135 RT_EXPORT_SYMBOL(RTStrPrintf2);136 137 138 126 RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, 139 127 const char *pszFormat, va_list args) … … 151 139 RT_EXPORT_SYMBOL(RTStrPrintf2ExV); 152 140 153 154 RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...)155 {156 va_list args;157 ssize_t cbRet;158 va_start(args, pszFormat);159 cbRet = RTStrPrintf2ExV(pfnFormat, pvArg, pszBuffer, cchBuffer, pszFormat, args);160 va_end(args);161 return cbRet;162 }163 RT_EXPORT_SYMBOL(RTStrPrintf2Ex);164
Note:
See TracChangeset
for help on using the changeset viewer.