-
-
Notifications
You must be signed in to change notification settings - Fork 424
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5d22e6
Added some new stocks
milutinke f817b9e
Removed unnecessary temporary variables.
milutinke 224de89
Update string.inc
milutinke 45ce1d6
string: normalize strings for SP.
KyleSanderson e94e682
Merge branch 'master' into milutinke/master
peace-maker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -557,3 +557,143 @@ 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; | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* 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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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