Raised This Month: $7 Target: $400
 1% 

[INC] ParseRange


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 05-23-2018 , 05:46   [INC] ParseRange
Reply With Quote #1

Simple include with a stock that parses ranges into the individual values within that range, contained within an ArrayList. Supports multiple ranges separated by commas, as well as ! meaning exceptions. Supports negative values.

Latest version always available at http://ddhoward.com/sourcemod/script...parseRange.inc

Example Input: "4-7, 12-22, !14-15, -3-3, -10--15"
Example Output: An ArrayList containing values -15 thru -10, -3 thru 7, 12 thru 13, and 16 thru 22.

PHP Code:
#if defined _ddhoward_parseRange_version
    #endinput
#endif
#define _ddhoward_parseRange_version "18.0523.1"

//http://ddhoward.com/sourcemod/scripting/include/parseRange.inc

/**
* Parses a string representing numerical ranges, and returns an ArrayList containing all applicable
* numbers. Supports ! symbol indicating exceptions. The string is parsed from left to right.
*
* Example input: "4-7, 12-22, !14-15, -3-3, -10--15"
* Example output: Array contains values -15 thru -10, -3 thru 7, 12 thru 13, and 16 thru 22
*
* @param sRange             String containing a range of integers, or multiple ranges separated by
*                           commas.
*
* @param soOrder            SortOrder representing how the resulting ArrayList should be sorted, or
*                           the default value _ for no sort.
*
* @param iMin               The minimum value that an integer can have and be placed in the Array.
*
* @param iMax               The maximum value that an integer can have and be placed in the Array.
*
* @param alPassedArray      Pre-existing ArrayList that you'd like this function to use. If you
*                           do not specify this, then a new one will be created and returned.
*
* @return                   An ArrayList containing all applicable integers. You muse close this
*                           Handle after you are done with it!
*/
stock ArrayList ParseRange(const char[] sRangeSortOrder soOrder view_as<SortOrder>(-1), int iMin = -2147483648int iMax 2147483647ArrayList alPassedArray null) {

    static 
bool prListenerCreated;
    if (!
prListenerCreated) {
        
AddCommandListener(print_parseRange_include_version"parseRange_include_versions");
        
prListenerCreated true;
    }

    
//calculate size of array needed
    
int iNumSections 1;
    for (
int cstrlen(sRange); c++) {
        if (
sRange[c] == ',') {
            
iNumSections++;
        }
    }

    
char[][] sSections = new char[iNumSections][25]; //strlen("!-2147483648--2147483648") + 1
    
iNumSections ExplodeString(sRange","sSectionsiNumSections25);
    
ArrayList alValues;
    if (
alPassedArray == null) {
        
alValues = new ArrayList();
    }
    else {
        
alValues alPassedArray;
    }

    for (
int iSectioniSection iNumSectionsiSection++) {
        
char sNumOne[12], sNumTwo[12];
        
bool bRemovebool bIsRange;
        for (
int cstrlen(sSections[iSection]); c++) {

            switch (
sSections[iSection][c]) {
                case 
'!' bRemove true;

                case 
'-' : {
                    if (
strlen(sNumOne) == 0) {
                        
sNumOne[0] = '-';
                    }
                    else if (!
bIsRange) {
                        
bIsRange true;
                    }
                    else if (
strlen(sNumTwo) == 0) {
                        
sNumTwo[0] = '-';
                    }
                }
                
                default: {
                    if (
IsCharNumeric(sSections[iSection][c])) {
                        if (!
bIsRange) {
                            
sNumOne[strlen(sNumOne)] = sSections[iSection][c];
                        }
                        else {
                            
sNumTwo[strlen(sNumTwo)] = sSections[iSection][c];
                        }
                    }
                }
            }

        }
        
int iFirst StringToInt(sNumOne);
        
int iSecond;

        if (
bIsRange) {
            
iSecond StringToInt(sNumTwo);
        }
        else {
            
iSecond iFirst;
        }

        if (
iFirst iSecond) {
            
int iTemp;
            
iTemp iFirst;
            
iFirst iSecond;
            
iSecond iTemp;
        }
        
        if (
iFirst iMin) {
            
iFirst iMin;
        }
        if (
iSecond iMax) {
            
iSecond iMax;
        }

        for (
int i iFirst<= iSecondi++) {
            
int iPrevIndex alValues.FindValue(i);
            if (
iPrevIndex == -&& !bRemove) {
                
alValues.Push(i);
            }
            else if (
iPrevIndex != -&& bRemove) {
                
alValues.Erase(iPrevIndex);
            }
        }
    }

    if (
soOrder view_as<SortOrder>(-1)) {
        
SortADTArray(alValuessoOrderSort_Integer);
    }
    return 
alValues;
}

//command listener which prints information to the chat, ALL plugins with this include will respond
//command user must have access to override "parseRange_include_versions" or root access to use this
public Action print_parseRange_include_version(int client, const char[] cmdnameint Args) {
    if (
client == || CheckCommandAccess(client"parseRange_include_versions"ADMFLAG_ROOTtrue)) {

        
char pluginFilename[PLATFORM_MAX_PATH];
        
GetPluginFilename(INVALID_HANDLEpluginFilenamesizeof(pluginFilename));

        
char pluginName[64];
        if (
GetPluginInfo(INVALID_HANDLEPlInfo_NamepluginNamesizeof(pluginName))) {
            
Format(pluginNamesizeof(pluginName), "%s%s"pluginName" - ");
        }

        
char pluginVersion[32];
        if (
GetPluginInfo(INVALID_HANDLEPlInfo_VersionpluginVersionsizeof(pluginVersion))) {
            
Format(pluginNamesizeof(pluginName), " %s"pluginName);
        }

        
        
ReplyToCommand(client"%s - %s%s%s"_ddhoward_parseRange_versionpluginNamepluginFilenamepluginVersion);
    }
    return 
Plugin_Continue;

The stock also adds a command listener for "parseRange_include_versions" which, when used by a root user, displays in that user's console a printout of all plugins using this include, and what version was used. This is a standard that I've created for use in all of my includes.
__________________

Last edited by ddhoward; 05-23-2018 at 16:19.
ddhoward is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 05-23-2018 , 20:47   Re: [INC] ParseRange
Reply With Quote #2

Awesome, thanks!
ThatKidWhoGames is offline
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 02:27.


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