Skip to content
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
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions plugins/include/string.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

@dvander dvander Apr 7, 2020

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.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

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.

Copy link
Author

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

{
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) {
Copy link
Member

Choose a reason for hiding this comment

The 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);
}