-
-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added some new string manipulation stocks #1224
Changes from 1 commit
c5d22e6
f817b9e
224de89
45ce1d6
e94e682
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -557,3 +557,149 @@ stock int ImplodeStrings(const char[][] strings, int numStrings, const char[] jo | |
} | ||
return total; | ||
} | ||
|
||
/** | ||
* Checks if the given string is an integer (Supports negative values) | ||
* | ||
* @param szString Input string | ||
* @return Boolean | ||
*/ | ||
stock bool IsStringNumber(const char[] szString) | ||
{ | ||
int iIterator = 0; | ||
int iLength = strlen(szString); | ||
|
||
char[] szTemporary = new char[iLength + 1]; | ||
strcopy(szTemporary, iLength + 1, szString); | ||
|
||
TrimString(szTemporary); | ||
|
||
if (szTemporary[0] == '-' && iLength > 1) | ||
++iIterator; | ||
|
||
while (szTemporary[iIterator] && IsCharNumeric(szTemporary[iIterator])) | ||
++iIterator; | ||
|
||
return szTemporary[iIterator] == 0 && iIterator != 0; | ||
} | ||
|
||
/** | ||
* 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; | ||
int iLength = strlen(szInput); | ||
char[] szOutput = new char[iLength + 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a temporary needed here, given that the string is modified in place? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, they are not needed. I am going to correct it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
while (szInput[iIterator] != EOS) | ||
{ | ||
if (!IsCharLower(szInput[iIterator]))szOutput[iIterator] = CharToLower(szInput[iIterator]); | ||
else szOutput[iIterator] = szInput[iIterator]; | ||
|
||
iIterator++; | ||
} | ||
|
||
szOutput[iIterator + 1] = EOS; | ||
strcopy(szInput, iLength + 1, szOutput); | ||
} | ||
|
||
/** | ||
* 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; | ||
int iLength = strlen(szInput); | ||
char[] szOutput = new char[iLength + 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. |
||
|
||
while (szInput[iIterator] != EOS) | ||
{ | ||
if (!IsCharUpper(szInput[iIterator]))szOutput[iIterator] = CharToUpper(szInput[iIterator]); | ||
else szOutput[iIterator] = szInput[iIterator]; | ||
|
||
iIterator++; | ||
} | ||
|
||
szOutput[iIterator + 1] = EOS; | ||
strcopy(szInput, iLength + 1, szOutput); | ||
} | ||
|
||
/** | ||
* Checks if the given string is a float (Supports negative values) | ||
* | ||
* @param szString Input string | ||
* @return Boolean | ||
*/ | ||
stock bool IsStringFloat(const char[] szString) | ||
{ | ||
char cCharacter; | ||
int iIterator; | ||
int iP; | ||
int iLength = strlen(szString); | ||
|
||
while (0 < szString[iIterator] <= 255) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be much easier to write this as a wrapper around StringToFloatEx(), which will also make sure the parsing is consistent. |
||
if (szString[0] == '-' && iLength > 1) { | ||
iIterator++; | ||
continue; | ||
} | ||
|
||
cCharacter = szString[iIterator]; | ||
|
||
if (!IsCharNumeric(cCharacter)) { | ||
if (cCharacter != '.' || iP) | ||
return false; | ||
|
||
iP = 1; | ||
} | ||
|
||
iIterator++; | ||
} | ||
|
||
return (iIterator > 2); | ||
} | ||
|
||
/** | ||
Headline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Finds the position (index) of character in the given string | ||
* | ||
* @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) | ||
{ | ||
int iTextLength = strlen(szText); | ||
int iCharacter, iLastIndex = -1; | ||
|
||
for (iCharacter = 0; iCharacter < iTextLength; iCharacter++) | ||
{ | ||
if (szText[iCharacter] == cCharacter) | ||
{ | ||
iLastIndex = iCharacter; | ||
|
||
if (!bLast) | ||
break; | ||
} | ||
} | ||
|
||
return iLastIndex; | ||
} | ||
|
||
/** | ||
Headline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Finds the last position (index) of character in the given string | ||
* | ||
* @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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need a temporary string? I think it would work to skip past spaces and then call StringToIntExt() and check the return value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never heard of StringToIntExt and I cannot find it in the documentation.
The reason I used a temporary value is to avoid modifying the original string when trimming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, StringToIntEx. You don't need to modify the string if you skip spaces with a counter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, I tried with StringToInt and StringToFloat and it did not work properly, the test which needed to pass, have failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results of those functions: https://i.imgur.com/4l1JzGd.png