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.

ParameterTypeDefault
textconst char*
separatorconst char*
maxPartsint0

Implode

virtual std::string Implode(const std::vector<std::string>& parts, const char* separator) = 0;
ParameterType
partsconst std::vector<std::string>&
separatorconst 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.

ParameterTypeDefault
textstd::string&
searchconst char*
replaceconst char*
caseSensitivebooltrue

Trim

virtual std::string Trim(const char* text) = 0;

Without the whitespace at both ends.

ParameterType
textconst 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.

ParameterTypeDefault
textconst char*
searchconst char*
caseSensitivebooltrue

StrEqual

virtual bool StrEqual(const char* a, const char* b, bool caseSensitive = true) = 0;
ParameterTypeDefault
aconst char*
bconst char*
caseSensitivebooltrue

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.

ParameterTypeDefault
formatconst char*
stampint64_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.

ParameterType
textconst char*
formatconst 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.

ParameterTypeDefault
patternconst char*
flagsint0
errorstd::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.

ParameterType
ownerPluginId
pathconst char*
messageconst 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.

ParameterTypeDefault
textconst char*
offsetsize_t0

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.

ParameterType
textconst 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).

ParameterTypeDefault
matchint0

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.

ParameterTypeDefault
indexint
matchint0

MatchEnd

virtual size_t MatchEnd(int match = 0) = 0;

Where a match ends in the text.

ParameterTypeDefault
matchint0

Release

virtual void Release() = 0;

On this page