ISource2ModUtils
Text, time, regex, profiling and file logging helpers.
The same helpers SourcePawn scripts get (source2mod/utils.inc), so a C++ plugin and a script that both write to a log, format a time or match a pattern get the same result.
Regex is ECMAScript (std::regex), not PCRE: SOURCE2MOD_REGEX_CASELESS and _DOTALL always apply, _MULTILINE where the C++ library has it.
Interface id ISource2ModUtils001 (SOURCE2MOD_UTILS_INTERFACE) · Global g_pSource2ModUtils · Header source2mod/ISource2ModUtils.h
Methods
Explode
virtual std::vector<std::string> Explode(const char* text, const char* separator, int maxParts = 0) = 0;Splits on @p separator (not on an empty one); @p maxParts > 0 caps the parts, the last keeps the rest.
| Parameter | Type | Default |
|---|---|---|
text | const char* | |
separator | const char* | |
maxParts | int | 0 |
Implode
virtual std::string Implode(const std::vector<std::string>& parts, const char* separator) = 0;| Parameter | Type |
|---|---|
parts | const std::vector<std::string>& |
separator | const char* |
Replace
virtual int Replace(std::string& text, const char* search, const char* replace, bool caseSensitive = true) = 0;Replaces every @p search. @return How many.
| Parameter | Type | Default |
|---|---|---|
text | std::string& | |
search | const char* | |
replace | const char* | |
caseSensitive | bool | true |
Trim
virtual std::string Trim(const char* text) = 0;Without the whitespace at both ends.
| Parameter | Type |
|---|---|
text | const char* |
Find
virtual int Find(const char* text, const char* search, bool caseSensitive = true) = 0;Where @p search starts in @p text; -1 when it is not there.
| Parameter | Type | Default |
|---|---|---|
text | const char* | |
search | const char* | |
caseSensitive | bool | true |
StrEqual
virtual bool StrEqual(const char* a, const char* b, bool caseSensitive = true) = 0;| Parameter | Type | Default |
|---|---|---|
a | const char* | |
b | const char* | |
caseSensitive | bool | true |
GetTime
virtual int64_t GetTime() = 0;Seconds since 1970.
FormatTime
virtual std::string FormatTime(const char* format, int64_t stamp = -1) = 0;strftime in the server's local time; @p stamp -1 is now.
| Parameter | Type | Default |
|---|---|---|
format | const char* | |
stamp | int64_t | -1 |
ParseTime
virtual int64_t ParseTime(const char* text, const char* format) = 0;The opposite, as local time. @return -1 when @p text does not fit @p format.
| Parameter | Type |
|---|---|
text | const char* |
format | const char* |
GetTickCount
virtual int64_t GetTickCount() = 0;Milliseconds on a steady clock (only differences mean anything).
CompileRegex
virtual ISource2ModRegex* CompileRegex(const char* pattern, int flags = 0, std::string* error = nullptr) = 0;nullptr (and why in @p error) for a bad pattern; Release() it.
| Parameter | Type | Default |
|---|---|---|
pattern | const char* | |
flags | int | 0 |
error | std::string* | nullptr |
LogToFile
virtual bool LogToFile(PluginId owner, const char* path, const char* message) = 0;Appends "L <date> - <time>: [<plugin>] <message>" to a file (under the game folder unless absolute); 0 for @p owner leaves the plugin out. Missing folders are made.
| Parameter | Type |
|---|---|
owner | PluginId |
path | const char* |
message | const char* |
ISource2ModRegex
A compiled pattern and its last matches.
Match
virtual int Match(const char* text, size_t offset = 0) = 0;Finds the first match at or after @p offset.
| Parameter | Type | Default |
|---|---|---|
text | const char* | |
offset | size_t | 0 |
Returns int — Substrings of the match (the whole match + its captures); 0 for no match.
MatchAll
virtual int MatchAll(const char* text) = 0;Finds every match. @return How many.
| Parameter | Type |
|---|---|
text | const char* |
MatchCount
virtual int MatchCount() = 0;Matches found by the last Match / MatchAll.
CaptureCount
virtual int CaptureCount(int match = 0) = 0;Substrings of a match (the whole match + its captures).
| Parameter | Type | Default |
|---|---|---|
match | int | 0 |
GetSubString
virtual const char* GetSubString(int index, int match = 0) = 0;Substring @p index of a match (0 = the whole match); nullptr when out of range.
| Parameter | Type | Default |
|---|---|---|
index | int | |
match | int | 0 |
MatchEnd
virtual size_t MatchEnd(int match = 0) = 0;Where a match ends in the text.
| Parameter | Type | Default |
|---|---|---|
match | int | 0 |
Release
virtual void Release() = 0;