RavEngine
Loading...
Searching...
No Matches
NvFlowString.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2014-2022 NVIDIA Corporation. All rights reserved.
26
27
28#pragma once
29
30#include <stdarg.h>
31#include "NvFlowTypes.h"
32
34
35struct NvFlowStringPool;
36
37NvFlowStringPool* NvFlowStringPoolCreate();
38
39char* NvFlowStringPoolAllocate(NvFlowStringPool* pool, NvFlowUint64 size);
40
41void NvFlowStringPoolTempAllocate(NvFlowStringPool* ptr, char** p_str_data, NvFlowUint64* p_str_size);
42
43void NvFlowStringPoolTempAllocateCommit(NvFlowStringPool* ptr, char* str_data, NvFlowUint64 str_size);
44
45void NvFlowStringPoolDestroy(NvFlowStringPool* pool);
46
47void NvFlowStringPoolReset(NvFlowStringPool* pool);
48
49char* NvFlowStringPrint(NvFlowStringPool* pool, const char* format, ...);
50
51char* NvFlowStringPrintV(NvFlowStringPool* pool, const char* format, va_list args);
52
54
55#define NV_FLOW_CSTR(X) NvFlowStringView{X, sizeof(X) - 1}
56
57#define NvFlowStringToInteger(input) atoi(input)
58
59#define NvFlowStringMakeView(input) NvFlowStringView{ input, (int)strlen(input) }
60
62
63NV_FLOW_INLINE int NvFlowCharIsWhiteSpace(char c)
64{
65 return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f' || c == '\v';
66}
67
68NV_FLOW_INLINE int NvFlowCharIsWhiteSpaceButNotNewline(char c)
69{
70 return c == ' ' || c == '\r' || c == '\t' || c == '\f' || c == '\v';
71}
72
73NV_FLOW_INLINE int NvFlowCharIsAlphaUnderscore(char c)
74{
75 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_');
76}
77
78NV_FLOW_INLINE int NvFlowCharIsAlphaNum(char c)
79{
80 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_');
81}
82
83NV_FLOW_INLINE int NvFlowCharIsNum(char c)
84{
85 return (c >= '0' && c <= '9');
86}
87
89
90NV_FLOW_INLINE NvFlowUint64 NvFlowStringLength(const char* a)
91{
92 if (!a)
93 {
94 return 0;
95 }
96 int idx = 0;
97 while (a[idx])
98 {
99 idx++;
100 }
101 return idx;
102}
103
104NV_FLOW_INLINE int NvFlowStringCompare(const char* a, const char* b)
105{
106 a = a ? a : "\0";
107 b = b ? b : "\0";
108 int idx = 0;
109 while (a[idx] || b[idx])
110 {
111 if (a[idx] != b[idx])
112 {
113 return a[idx] < b[idx] ? -1 : +1;
114 }
115 idx++;
116 }
117 return 0;
118}
119
120NV_FLOW_INLINE char* NvFlowStringFromView(NvFlowStringPool* pool, const char* data, NvFlowUint64 size)
121{
122 char* str = NvFlowStringPoolAllocate(pool, size);
123 for (NvFlowUint64 i = 0; i < size; i++)
124 {
125 str[i] = data[i];
126 }
127 return str;
128}
129
130NV_FLOW_INLINE void NvFlowStringSplitDelimFirst(NvFlowStringPool* pool, char** pFirst, char** pSecond, const char* input_data, char delim)
131{
132 NvFlowUint64 input_size = NvFlowStringLength(input_data);
133 NvFlowUint64 slashIdx = 0;
134 while (slashIdx < input_size)
135 {
136 if (input_data[slashIdx] == delim)
137 {
138 break;
139 }
140 slashIdx++;
141 }
142 *pFirst = NvFlowStringFromView(pool, input_data, slashIdx + 1);
143 *pSecond = NvFlowStringFromView(pool, input_data + slashIdx + 1, input_size - slashIdx - 1);
144}
145
146NV_FLOW_INLINE void NvFlowStringSplitDelimLast(NvFlowStringPool* pool, char** pFirst, char** pSecond, const char* input_data, char delim)
147{
148 NvFlowUint64 input_size = NvFlowStringLength(input_data);
149 NvFlowUint64 slashIdx = input_size - 1;
150 while (slashIdx < input_size)
151 {
152 if (input_data[slashIdx] == delim)
153 {
154 break;
155 }
156 slashIdx--;
157 }
158 *pFirst = NvFlowStringFromView(pool, input_data, slashIdx + 1);
159 *pSecond = NvFlowStringFromView(pool, input_data + slashIdx + 1, input_size - slashIdx - 1);
160}
161
162NV_FLOW_INLINE char* NvFlowStringDup(NvFlowStringPool* pool, const char* name)
163{
164 NvFlowUint64 name_size = NvFlowStringLength(name);
165 return NvFlowStringFromView(pool, name, name_size);
166}
167
168NV_FLOW_INLINE char* NvFlowStringConcat(NvFlowStringPool* pool, const char* dir_data, const char* filename_data)
169{
170 NvFlowUint64 dir_size = NvFlowStringLength(dir_data);
171 NvFlowUint64 filename_size = NvFlowStringLength(filename_data);
172 char* s_data = NvFlowStringPoolAllocate(pool, dir_size + filename_size);
173 for (NvFlowUint64 i = 0; i < dir_size; i++)
174 {
175 s_data[i] = dir_data[i];
176 }
177 for (NvFlowUint64 i = 0; i < filename_size; i++)
178 {
179 s_data[i + dir_size] = filename_data[i];
180 }
181 return s_data;
182}
183
184NV_FLOW_INLINE char* NvFlowStringConcatN(NvFlowStringPool* pool, const char** views, NvFlowUint64 numViews)
185{
186 NvFlowUint64 totalSize = 0;
187 for (NvFlowUint64 viewIdx = 0; viewIdx < numViews; viewIdx++)
188 {
189 totalSize += NvFlowStringLength(views[viewIdx]);
190 }
191 char* s_data = NvFlowStringPoolAllocate(pool, totalSize);
192 NvFlowUint64 dstOffset = 0;
193 for (NvFlowUint64 viewIdx = 0; viewIdx < numViews; viewIdx++)
194 {
195 const char* view_data = views[viewIdx];
196 NvFlowUint64 view_size = NvFlowStringLength(view_data);
197 for (NvFlowUint64 i = 0; i < view_size; i++)
198 {
199 s_data[i + dstOffset] = view_data[i];
200 }
201 dstOffset += view_size;
202 }
203 return s_data;
204}
205
206NV_FLOW_INLINE char* NvFlowStringConcat3(NvFlowStringPool* pool, const char* a, const char* b, const char* c)
207{
208 const char* list[3u] = { a, b, c };
209 return NvFlowStringConcatN(pool, list, 3u);
210}
211
212NV_FLOW_INLINE char* NvFlowStringConcat4(NvFlowStringPool* pool, const char* a, const char* b, const char* c, const char* d)
213{
214 const char* list[4u] = { a, b, c, d };
215 return NvFlowStringConcatN(pool, list, 4u);
216}
217
218NV_FLOW_INLINE char* NvFlowStringTrimEnd(NvFlowStringPool* pool, const char* a_data, char trimChar)
219{
220 NvFlowUint64 a_size = NvFlowStringLength(a_data);
221 while (a_size > 0 && a_data[a_size - 1] == trimChar)
222 {
223 a_size--;
224 }
225 return NvFlowStringFromView(pool, a_data, a_size);
226}
227
228NV_FLOW_INLINE char* NvFlowStringTrimBeginAndEnd(NvFlowStringPool* pool, const char* a_data, char trimChar)
229{
230 NvFlowUint64 a_size = NvFlowStringLength(a_data);
231 while (a_size > 0 && a_data[0] == trimChar)
232 {
233 a_data++;
234 a_size--;
235 }
236 while (a_size > 0 && a_data[a_size - 1] == trimChar)
237 {
238 a_size--;
239 }
240 return NvFlowStringFromView(pool, a_data, a_size);
241}
242
244
245const char* NvFlowTextFileLoad(NvFlowStringPool* pool, const char* filename);
246
247void NvFlowTextFileStore(const char* text, const char* filename);
248
249NvFlowBool32 NvFlowTextFileTestOpen(const char* filename);
250
251void NvFlowTextFileRemove(const char* name);
252
253void NvFlowTextFileRename(const char* oldName, const char* newName);
254
255NvFlowBool32 NvFlowTextFileDiffAndWriteIfModified(const char* filenameDst, const char* filenameTmp);
Definition NvFlowString.cpp:35