-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstr.h
232 lines (192 loc) · 10 KB
/
str.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*!
* @file libxutils/src/data/str.h
*
* This source is part of "libxutils" project
* 2015-2020 Sun Dro ([email protected])
*
* @brief This source includes string operations.
*/
#ifndef __XUTILS_XSTR_H__
#define __XUTILS_XSTR_H__
#include "xstd.h"
#include "array.h"
#include "pool.h"
/* Supported colors */
#define XSTR_CLR_NONE "\x1B[0m"
#define XSTR_CLR_RED "\x1B[31m"
#define XSTR_CLR_GREEN "\x1B[32m"
#define XSTR_CLR_YELLOW "\x1B[33m"
#define XSTR_CLR_BLUE "\x1B[34m"
#define XSTR_CLR_MAGENTA "\x1B[35m"
#define XSTR_CLR_CYAN "\x1B[36m"
#define XSTR_CLR_WHITE "\x1B[37m"
#define XSTR_CLR_LIGHT_RED "\x1B[31;1m"
#define XSTR_CLR_LIGHT_GREEN "\x1B[32;1m"
#define XSTR_CLR_LIGHT_YELLOW "\x1B[33;1m"
#define XSTR_CLR_LIGHT_BLUE "\x1B[34;1m"
#define XSTR_CLR_LIGHT_MAGENTA "\x1B[35;1m"
#define XSTR_CLR_LIGHT_CYAN "\x1B[36;1m"
#define XSTR_CLR_LIGHT_WHITE "\x1B[37;1m"
/* Supported background colors */
#define XSTR_BACK_BLACK "\x1B[40m"
#define XSTR_BACK_RED "\x1B[41m"
#define XSTR_BACK_GREEN "\x1B[42m"
#define XSTR_BACK_YELLOW "\x1B[43m"
#define XSTR_BACK_BLUE "\x1B[44m"
#define XSTR_BACK_MAGENTA "\x1B[45m"
#define XSTR_BACK_CYAN "\x1B[46m"
#define XSTR_BACK_WHITE "\x1B[47m"
/* Supported string formats */
#define XSTR_FMT_BOLD "\x1B[1m"
#define XSTR_FMT_DIM "\x1B[2m"
#define XSTR_FMT_ITALIC "\x1B[3m"
#define XSTR_FMT_ULINE "\x1B[4m"
#define XSTR_FMT_FLICK "\x1B[5m"
#define XSTR_FMT_BLINK "\x1B[6m"
#define XSTR_FMT_HIGHLITE "\x1B[7m"
#define XSTR_FMT_HIDE "\x1B[8m"
#define XSTR_FMT_CROSS "\x1B[9m"
#define XSTR_FMT_RESET XSTR_CLR_NONE
#define XSTR_SCREEN_CLEAR "\033[H\033[J"
#define XSTR_DOUBLE_ARG_MAX 309
#define XSTR_INT_ARG_MAX 32
#define XSTR_LETTERS 52
#define XSTR_MAX 8192
#define XSTR_MID 4096
#define XSTR_MIN 2048
#define XSTR_TINY 256
#define XSTR_MICRO 32
#define XSTR_NPOS UINT_MAX
#define XSTR_STACK XSTR_MID
#define XSTR_SPACE_CHAR ' '
#define XSTR_NEW_LINE "\n"
#define XSTR_SPACE " "
#define XSTR_EMPTY ""
#define XSTR_NUL '\0'
#define XSTR_INIT { XSTR_NUL }
#define XSTR_AVAIL(arr)((int)sizeof(arr)-(int)strlen(arr))
#define XSTR_NAVAIL(arr,n)((int)n-(int)strlen(arr))
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
XSTR_LOWER = 0,
XSTR_UPPER
} xstr_case_t;
/////////////////////////////////////////////////////////////////////////
// C string functions
/////////////////////////////////////////////////////////////////////////
size_t xstrsplita(const char *pString, const char *pDlmt, xarray_t *pTokens, xbool_t bIncludeDlmt, xbool_t bIncludeEmpty);
xarray_t* xstrsplit(const char *pString, const char *pDlmt);
xarray_t* xstrsplitd(const char *pString, const char *pDlmt);
xarray_t* xstrsplite(const char *pString, const char *pDlmt);
char* xstrrep(const char *pOrig, const char *pRep, const char *pWith);
int xstrnrep(char *pDst, size_t nSize, const char *pOrig, const char *pRep, const char *pWith);
char *xstrpdup(xpool_t *pPool, const char *pStr);
char* xstrdup(const char *pStr);
char* xstracpy(const char *pFmt, ...);
char* xstracpyn(size_t *nSize, const char *pFmt, ...);
char* xstralloc(size_t nSize);
int xstrncpyarg(char *pDest, size_t nSize, const char *pFmt, va_list args);
char* xstrpcpyargs(xpool_t *pPool, const char *pFmt, va_list args, size_t *nSize);
char* xstracpyarg(const char *pFmt, va_list args);
char* xstracpyargs(const char *pFmt, va_list args, size_t *nSize);
size_t xstrarglen(const char *pFmt, va_list args);
size_t xstrxcpyf(char **pDst, const char *pFmt, ...);
char* xstrxcpy(const char *pFmt, ...);
xbool_t xstrmatchm(const char *pStr, size_t nLength, const char *pPattern, const char *pDelimiter);
xbool_t xstrnmatch(const char *pStr, size_t nLength, const char *pPattern, size_t nPatternLength);
xbool_t xstrmatch(const char *pStr, size_t nLength, const char *pPattern);
xbool_t xstrregex(const char *pStr, size_t nLength, const char *pPattern);
xbool_t xstrncmpn(const char *pStr, size_t nStrLen, const char *pCmp, size_t nCmpLen);
xbool_t xstrncmp(const char *pStr, const char *pCmp, size_t nCmpLen);
xbool_t xstrcmp(const char *pStr, const char *pCmp);
size_t xstrncpy(char *pDst, size_t nSize, const char* pSrc);
size_t xstrncpys(char *pDst, size_t nDstSize, const char *pSrc, size_t nSrcLen);
size_t xstrncpyf(char *pDst, size_t nSize, const char *pFmt, ...);
size_t xstrncat(char *pDst, size_t nSize, const char *pFmt, ...);
size_t xstrncatf(char *pDst, size_t nAvail, const char *pFmt, ...);
size_t xstrncatsf(char *pDst, size_t nSize, size_t nAvail, const char *pFmt, ...);
size_t xstrrand(char *pDst, size_t nSize, size_t nLength, xbool_t bUpper, xbool_t bNumbers);
size_t xstrextra(const char *pStr, size_t nLength, size_t nMaxChars, size_t *pChars, size_t *pPosit);
size_t xstrncpyfl(char *pDst, size_t nSize, size_t nFLen, char cFChar, const char* pFmt, ...);
size_t xstrnlcpyf(char *pDst, size_t nSize, size_t nFLen, char cFChar, const char* pFmt, ...);
size_t xstrnfill(char *pDst, size_t nSize, size_t nLength, char cFill);
size_t xstrisextra(const char *pOffset);
char *xstrfill(size_t nLength, char cFill);
int xstrnsrc(const char *pStr, size_t nLen, const char *pSrc, size_t nPos);
int xstrsrcb(const char *pStr, size_t nLength, const char *pSrc);
int xstrsrcp(const char *pStr, const char *pSrc, size_t nPos);
int xstrsrc(const char *pStr, const char *pSrc);
char* xstracase(const char *pSrc, xstr_case_t eCase);
char* xstracasen(const char *pSrc, xstr_case_t eCase, size_t nLength);
size_t xstrcase(char *pSrc, xstr_case_t eCase);
size_t xstrncase(char *pDst, size_t nSize, xstr_case_t eCase, const char *pSrc);
size_t xstrncases(char* pDst, size_t nSize, xstr_case_t eCase, const char *pSrc, size_t nSrcLen);
char* xstrtok(char* pString, const char* pDelimiter, char** pContext);
char* xstrcut(char *pData, const char *pStart, const char *pEnd);
char* xstracut(const char *pSrc, size_t nPosit, size_t nSize);
size_t xstrncut(char *pDst, size_t nDstSize, const char *pData, size_t nPosit, size_t nSize);
size_t xstrncuts(char *pDst, size_t nSize, const char *pSrc, const char *pStart, const char *pEnd);
int xstrntok(char *pDst, size_t nSize, const char *pStr, size_t nPosit, const char *pDlmt);
size_t xstrnclr(char *pDst, size_t nSize, const char* pClr, const char* pStr, ...);
size_t xstrnrm(char *pStr, size_t nPosit, size_t nSize);
void xstrnull(char *pString, size_t nLength);
void xstrnul(char *pString);
xbool_t xstrused(const char *pStr);
char* xstrtoen(char *pBuffer, size_t nSize, const char *pStr);
char* xstrtoge(char *pBuffer, size_t nSize, const char *pStr);
size_t xstrnrgb(char *pStr, size_t nSize, int nR, int nG, int nB);
size_t xstrnyuv(char *pStr, size_t nSize, int nY, int nU, int nV);
char *xstrrgb(int nR, int nG, int nB);
char *xstryuv(int nY, int nU, int nV);
/////////////////////////////////////////////////////////////////////////
// XString Implementation
/////////////////////////////////////////////////////////////////////////
typedef struct XString {
char* pData;
size_t nLength;
size_t nSize;
int16_t nStatus;
xbool_t nAlloc;
xbool_t nFast;
} xstring_t;
xstring_t *XString_New(size_t nSize, uint8_t nFastAlloc);
xstring_t *XString_From(const char *pData, size_t nLength);
xstring_t *XString_FromFmt(const char *pFmt, ...);
xstring_t *XString_FromStr(xstring_t *pString);
int XString_Init(xstring_t *pString, size_t nSize, uint8_t nFastAlloc);
int XString_InitFrom(xstring_t *pStr, const char *pFmt, ...);
int XString_Set(xstring_t *pString, char *pData, size_t nLength);
int XString_Increase(xstring_t *pString, size_t nSize);
int XString_Resize(xstring_t *pString, size_t nSize);
void XString_Clear(xstring_t *pString);
int XString_Add(xstring_t *pString, const char *pData, size_t nLength);
int XString_Copy(xstring_t *pString, xstring_t *pSrc);
int XString_Append(xstring_t *pString, const char *pFmt, ...);
int XString_AddString(xstring_t *pString, xstring_t *pSrc);
int XString_Insert(xstring_t *pString, size_t nPosit, const char *pData, size_t nLength);
int XString_InsertFmt(xstring_t *pString, size_t nPosit, const char *pFmt, ...);
int XString_Remove(xstring_t *pString, size_t nPosit, size_t nSize);
int XString_Delete(xstring_t *pString, size_t nPosit, size_t nSize);
int XString_Advance(xstring_t *pString, size_t nSize);
int XString_Tokenize(xstring_t *pString, char *pDst, size_t nSize, size_t nPosit, const char *pDlmt);
int XString_Token(xstring_t *pString, xstring_t *pDst, size_t nPosit, const char *pDlmt);
int XString_Case(xstring_t *pString, xstr_case_t eCase, size_t nPosit, size_t nSize);
int XString_Color(xstring_t *pString, const char* pClr, size_t nPosit, size_t nSize);
int XString_ChangeCase(xstring_t *pString, xstr_case_t eCase);
int XString_ChangeColor(xstring_t *pString, const char* pClr);
int XString_Replace(xstring_t *pString, const char *pRep, const char *pWith);
int XString_Search(xstring_t *pString, size_t nPos, const char *pSearch);
xarray_t* XString_SplitStr(xstring_t *pString, const char *pDlmt);
xarray_t* XString_Split(const char *pCStr, const char *pDlmt);
int XString_Sub(xstring_t *pString, char *pDst, size_t nDstSize, size_t nPos, size_t nSize);
int XString_SubStr(xstring_t *pString, xstring_t *pSub, size_t nPos, size_t nSize);
xstring_t *XString_SubNew(xstring_t *pString, size_t nPos, size_t nSize);
int XString_Cut(xstring_t *pString, char *pDst, size_t nSize, const char *pFrom, const char *pTo);
int XString_CutSub(xstring_t *pString, xstring_t *pSub, const char *pFrom, const char *pTo);
xstring_t *XString_CutNew(xstring_t *pString, const char *pFrom, const char *pTo);
#ifdef __cplusplus
}
#endif
#endif /* __XUTILS_XSTR_H__ */