Raised This Month: $12 Target: $400
 3% 

String Manipulation Functions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 04-05-2020 , 14:45   String Manipulation Functions
Reply With Quote #1

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[] strint nBase=10) {
    
int result;
    return 
StringToIntEx(strresultnBase) == 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(strresult) == 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 cCharacterbool bLast false) {
    return 
FindCharInString(szTextcCharacterbLast);
}

/**
 * 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(szTextcCharactertrue);
}

/**
 * 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[] szInputchar[] szOutputint iOutputLengthchar cCharacter) {
    
int iInputIteratoriOutputIterator;
    
    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.

Last edited by milutinke; 04-10-2020 at 19:18.
milutinke is offline
Send a message via Skype™ to milutinke
JoinedSenses
Senior Member
Join Date: Sep 2013
Old 04-05-2020 , 22:53   Re: String Manipulation Functions
Reply With Quote #2

Code:
// Fuzzy search
stock int FuzzyCompare(const char[] needle, const char[] haystack) {
	int hlen = strlen(haystack);
	int nlen = strlen(needle);

	if (nlen > hlen) {
		return false;
	}
	if (nlen == hlen) {
		return strcmp(needle, haystack) == 0;
	}

	int n = 0;
	int h = 0;
	int p = 0;

	for (; n < nlen; n++) {
		int nch = needle[n];

		while (h < hlen) {
			if (nch == haystack[h]) {
				h++;
				p++;
				break;
			}
			h++;
		}
	}

	return (p == nlen);
}

enum DataType {
	DATAINT,
	DATAFLOAT,
	DATASTRING
}

// checks if input is float, int, or string
stock DataType CheckType(const char[] input, int len) {
	if (!input[0] || !len) {
		return DATASTRING;
	}

	int numbers = 0;
	int dot = 0;
	char c;
	for (int i = (input[0] == '-'); i < len; i++) {
		c = input[i];
		if (IsCharNumeric(c)) {
			numbers++;
		}
		else if (c == '.' && !dot) {
			dot++
		}
		else if (!c) {
			break;
		}
		else {
			return DATASTRING;
		}
	}

	return dot ? DATAFLOAT : DATAINT;
}

stock void HexToRGB(const char[] hex, int rgb[3]) {
	int hexInt = StringToInt(hex, 16);
	rgb[0] = ((hexInt >> 16) & 0xFF);
	rgb[1] = ((hexInt >> 8) & 0xFF);
	rgb[2] = ((hexInt >> 0) & 0xFF);
}

stock void RGBToHexStr(int rgb[3], char[] hexstr, int size) {
	int hex; 
	hex |= ((rgb[0] & 0xFF) << 16);
	hex |= ((rgb[1] & 0xFF) <<  8);
	hex |= ((rgb[2] & 0xFF) <<  0);

	Format(hexstr, size, "%6X", hex);
}
__________________

Last edited by JoinedSenses; 04-05-2020 at 22:54.
JoinedSenses is offline
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 04-08-2020 , 12:44   Re: String Manipulation Functions
Reply With Quote #3

Added copyc and improved StringToUpperCase and StringToLowerCase

PHP Code:
/**
 * 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[] szInputchar[] szOutputint iOutputLengthchar cCharacter) {
    
int iInputIteratoriOutputIterator;
    
    while(
szInput[iInputIterator] != EOS && iOutputIterator iOutputLength && szInput[iInputIterator] != cCharacter)
        
szOutput[iOutputIterator ++] = szInput[iInputIterator ++];
    
    
szOutput[iOutputIterator] = EOS;


Last edited by milutinke; 04-10-2020 at 19:10.
milutinke is offline
Send a message via Skype™ to milutinke
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:32.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode