Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
StringSub failing in the background?
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#1
StringSub failing in the background?

Assume the following code. Testing shows that "Passed1" gets printed to screen while "Passed2" doesn't. It does not fail to find the needle and it properly returns the index that it found the delimiter at. Why does it seem to end at StringSub()?

void ChangeMap(string &in map)
    {
        string[] split = StringSplit(map, ":");

        if (split.length() > 1)
        {
            AddDebugMessage(split[0], false);
            AddDebugMessage(split[1], false);
//            ChangeMap(split[0], split[1], "", "");
        }

        else AddDebugMessage("String array length less than 1", false);
    }

void OnEnter()
    {
        AddTimer("creed_of_amnesia.map:StartMenu", 3, "ChangeMap");
    }

int StringFindFirst(string &in heystack, string &in needle, int start)
    {
        if (StringContains(heystack, needle))
        {
            if (start < 0)
                start = 0;

            for (int i = start; i < heystack.length(); ++i)
            {
                if (StringSub(heystack, i, 1) == needle)
                {
                    AddDebugMessage("Found needle: "+needle, false);
                    return i;
                }
            }
        }

        else AddDebugMessage("No needle in heystack", false);

        AddDebugMessage("Couldn't find needle in heystack", false);
        return -1;
    }

string[] StringSplit(string &in str, string &in delimiter)
    {
        string[] ret;

        int start = 0;
        int i = StringFindFirst(str, delimiter, start);

        string str_i = i;
        str_i += " "+str.length();
        AddDebugMessage(str_i, false);

        while (i != -1)
        {
            if (str.length() < start
            || str.length() < i)
                break;

            AddDebugMessage("Passed1", false);
            ret[ret.length()] = StringSub(str, start, i); // Seems to be haulting
            AddDebugMessage("Passed2", false);

            start = i+1;
            i = StringFindFirst(str, delimiter, start);
        }

        return ret;
    }

int StringToInt(string &in str)
    {
        if (str.length() > 0)
        {
            int i = 0;
            string chk_str = i;

            while (str != chk_str)
            {
                ++i;
                chk_str = i;
            }

            return i;
        }

        return 0;
    }

Tutorials: From Noob to Pro
(This post was last modified: 09-27-2011, 12:39 AM by Your Computer.)
09-26-2011, 11:29 PM
Website Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#2
RE: StringSub failing in the background?

Your array "ret" is empty from my quick glance, and you are also seem to be acessing the n+1th element of the array. I changed it's definition to:

string[] ret = {""};
and the problem line to
ret[ret.length() - 1] = StringSub(str, start, i); // Seems to be haulting
and it worked. After this, since i am assuming you want to split the string based on the delimiter into elements of the array, an:
ret.resize(ret.length() + 1);
Would help out with that.

Hope that helps Wink. I've also built a string splitting system based on 1 char delimiters - it's in the "string list" stuff in the wiki thread if you want to take a gander - though I doubt you'd have much to gain from it as your approach would support delims of an arbitrary length.

Edit:
Also, your int parsing function will crash the game if the assumed invariant is broken and a negative number or non-int is passed in. I think parsing character-by-character is probably going to be a better approach here than looping up to the number (it will certainly be more efficient as number size grows). I've been using this parse I wrote for a while now (refining it quite a bit since it's first appearance on the forums) - it may help you out a little.
//Helper function: Used to determine if the char "digit" represents a number
//Returns: (-1) if not a number. Else returns the digit (0-9).
int  _ParseDigit(uint8 digit) {
    int d = digit-48; //48 is ASCII code for 0
    return ((d >= 0)&&(d<=9)) ? d : -1; }

//Parses ALL the numbers from a string on a by-char basis.
//Sign ignored. No numbers returns 0.
uint parseStringUInt(string &in asString) {
    uint output = 0;
    for(uint i=0; i<asString.length(); i++)
     {
       int digit = _ParseDigit(asString[i]); //Get the digit represented by char at i (-1 if not digit)
       output = (digit > -1)?(10*output+digit):(output); //Append digit to end of output num if char is a digit.
     }
    return output;
}
(This post was last modified: 09-27-2011, 12:31 AM by Apjjm.)
09-27-2011, 12:14 AM
Find
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#3
RE: StringSub failing in the background?

Hmm, i was hoping it would resize automatically. I guess my knowledge of other scripting languages isn't entirely applicable here.

Yeah, i know my StringToInt function will crash or continue indefinitely (until the system runs out of memory) if an inappropriate value is passed in, but i'm not worried about that for my simple purpose (if someone else, however, were to use it, then it may be problematic). I will try out your string conversion functions, though. (I was aiming more for PHP's way of string-to-int juggling for my purpose.)

Tutorials: From Noob to Pro
09-27-2011, 12:38 AM
Website Find




Users browsing this thread: 1 Guest(s)