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
|
#include "slang-string-util.h"
namespace Slang {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StringBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!
// Allocate static const storage for the various interface IDs that the Slang API needs to expose
static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;
static const Guid IID_ISlangBlob = SLANG_UUID_ISlangBlob;
/* static */ISlangUnknown* StringBlob::getInterface(const Guid& guid)
{
return (guid == IID_ISlangUnknown || guid == IID_ISlangBlob) ? static_cast<ISlangBlob*>(this) : nullptr;
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StringUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!
/* static */void StringUtil::split(const UnownedStringSlice& in, char splitChar, List<UnownedStringSlice>& slicesOut)
{
slicesOut.Clear();
const char* start = in.begin();
const char* end = in.end();
while (start < end)
{
// Move cur so it's either at the end or at next split character
const char* cur = start;
while (cur < end && *cur != splitChar)
{
cur++;
}
// Add to output
slicesOut.Add(UnownedStringSlice(start, cur));
// Skip the split character, if at end we are okay anyway
start = cur + 1;
}
}
/* static */size_t StringUtil::calcFormattedSize(const char* format, va_list args)
{
#if SLANG_WINDOWS_FAMILY
return _vscprintf(format, args);
#else
return vsnprintf(nullptr, 0, format, args);
#endif
}
/* static */void StringUtil::calcFormatted(const char* format, va_list args, size_t numChars, char* dst)
{
#if SLANG_WINDOWS_FAMILY
vsnprintf_s(dst, numChars + 1, _TRUNCATE, format, args);
#else
vsnprintf(dst, numChars + 1, format, args);
#endif
}
/* static */void StringUtil::append(const char* format, va_list args, StringBuilder& buf)
{
// Calculate the size required (not including terminating 0)
size_t numChars;
{
// Create a copy of args, as will be consumed by calcFormattedSize
va_list argsCopy;
va_copy(argsCopy, args);
numChars = calcFormattedSize(format, argsCopy);
va_end(argsCopy);
}
// Requires + 1 , because calcFormatted appends a terminating 0
char* dst = buf.prepareForAppend(numChars + 1);
calcFormatted(format, args, numChars, dst);
buf.appendInPlace(dst, numChars);
}
/* static */void StringUtil::appendFormat(StringBuilder& buf, const char* format, ...)
{
va_list args;
va_start(args, format);
append(format, args, buf);
va_end(args);
}
/* static */String StringUtil::makeStringWithFormat(const char* format, ...)
{
StringBuilder builder;
va_list args;
va_start(args, format);
append(format, args, builder);
va_end(args);
return builder;
}
/* static */String StringUtil::getString(ISlangBlob* blob)
{
if (blob)
{
size_t size = blob->getBufferSize();
if (size > 0)
{
const char* contents = (const char*)blob->getBufferPointer();
// Check it has terminating 0, if not we must construct as if it does
if (contents[size - 1] == 0)
{
size--;
}
return String(contents, contents + size);
}
}
return String();
}
ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string)
{
return ComPtr<ISlangBlob>(new StringBlob(string));
}
/* static */String StringUtil::calcCharReplaced(const UnownedStringSlice& slice, char fromChar, char toChar)
{
if (fromChar == toChar)
{
return slice;
}
const UInt numChars = slice.size();
const char* srcChars = slice.begin();
StringBuilder builder;
char* dstChars = builder.prepareForAppend(numChars);
for (UInt i = 0; i < numChars; ++i)
{
char c = srcChars[i];
dstChars[i] = (c == fromChar) ? toChar : c;
}
builder.appendInPlace(dstChars, numChars);
return builder;
}
/* static */String StringUtil::calcCharReplaced(const String& string, char fromChar, char toChar)
{
return (fromChar == toChar || string.IndexOf(fromChar) == UInt(-1)) ? string : calcCharReplaced(string.getUnownedSlice(), fromChar, toChar);
}
} // namespace Slang
|