I have started to make one plugin where I need to do some string manipulation, however, I have not found the following function in the Source Pawn, so I have decided to make them myself or port some from the AMXX.
I hope that someone will find them useful.
PHP Code:
/**
* Checks if the given string is an integer (Supports negative values)
*
* @param szString Input string
* @return Boolean
*/
stock bool IsStringNumber(const char[] str, int nBase=10) {
int result;
return StringToIntEx(str, result, nBase) == strlen(str);
}
/**
* Checks if the given string is a float (Supports negative values)
*
* @param szString Input string
* @return Boolean
*/
stock bool IsStringFloat(const char[] str) {
float result;
return StringToFloatEx(str, result) == strlen(str) && FindCharInString(str, '.') != -1;
}
/**
* Converts the given string to lower case
*
* @param szString Input string for conversion and also the output
* @return void
*/
stock void StringToLowerCase(char[] szInput) {
int iIterator = 0;
while (szInput[iIterator] != EOS) {
if (!IsCharLower(szInput[iIterator])) szInput[iIterator] = CharToLower(szInput[iIterator]);
else szInput[iIterator] = szInput[iIterator];
iIterator++;
}
szInput[iIterator + 1] = EOS;
}
/**
* Converts the given string to upper case
*
* @param szString Input string for conversion and also the output
* @return void
*/
stock void StringToUpperCase(char[] szInput) {
int iIterator = 0;
while (szInput[iIterator] != EOS) {
if (!IsCharUpper(szInput[iIterator])) szInput[iIterator] = CharToUpper(szInput[iIterator]);
else szInput[iIterator] = szInput[iIterator];
iIterator++;
}
szInput[iIterator + 1] = EOS;
}
/**
* Finds the position (index) of character in the given string (A wrapper around FindCharInString)
*
* @param szString Input string
* @param szString Input character
* @param szString Do we need the last index (Optional, default: false)
* @return Integer, index of the found character (returns -1 if the character was not found)
*/
stock int IndexOfChar(const char[] szText, const char cCharacter, bool bLast = false) {
return FindCharInString(szText, cCharacter, bLast);
}
/**
* Finds the last position (index) of character in the given string (A wrapper around IndexOfChar)
*
* @param szString Input string
* @param szString Input character
* @return Integer, index of the found character. (returns -1 if the character was not found)
*/
stock int LastIndexOfChar(const char[] szText, const char cCharacter) {
return IndexOfChar(szText, cCharacter, true);
}
/**
* Copies the input string into the output string until it reaches the given character
*
* @param szInput Input string
* @param szOutput Output string
* @param iOutputLength Output max length
* @param cCharacter Character
*
* @return void
*/
stock void CopyC(const char[] szInput, char[] szOutput, int iOutputLength, char cCharacter) {
int iInputIterator, iOutputIterator;
while(szInput[iInputIterator] != EOS && iOutputIterator < iOutputLength && szInput[iInputIterator] != cCharacter)
szOutput[iOutputIterator ++] = szInput[iInputIterator ++];
szOutput[iOutputIterator] = EOS;
}
Edit: Changed the functions to wrap some existing ones.