views:

279

answers:

2

Is there a way to force a container to store all values as strings? I am using str2con in order to split text strings into containers. Any time a field with numbers only comes up, it is stored as an int, which isn't a huge problem. What IS a big problem is when the string of numbers exceeds the integer size and the number becomes something different.

Consider the following strings:

"Text1,Text2"      Container becomes: str "Text1", str "Text2"
"1111111111,Text"   Container becomes: int 1111111111, str "Text"   
"8888888888,Text"   Container becomes: int -961633963, str "Text"  (THIS IS BAD)

Any suggestions for how to get around this?

Thanks

+1  A: 

You can see how Microsoft implemented str2con by looking at the Global.str2con method. To keep the method from adding integers to a container, make a copy of the method and just comment out the three lines in the add2Ret sub function that check if the string is only digits. You probably don't want to modify the existing str2con function as other parts of the system may depend on integers actually being integers when calling this method.

void add2Ret(str _current)
{  
    // v-artemt, 26 Jul 2004, PS#: 1741
 //remove next three lines so only integers will be added as strings not integers
 //   if (match('<:d+>', _current))
 //       ret += str2int(_current);
 //   else
        ret += _current;
}

Or you could add more complicated logic to check the length of the string and only use str2int if the string could possible fit into an integer.

Jay Hofacker
Thanks! Worked perfectly... after realizing that the Global class is not in the Gs, but at the bottom of the list. ;)
Brad
+1  A: 

Here is alternative implementation:

#define.Comma(",")

static container str2con_alt(str _string, str _separator = #Comma, boolean  _ignoreNearSeparator = false)
{
  container con = connull();
  int       pos, oldPos = 1;
  str       tmpStr;

  do
  {
      pos    =  strscan(_string, _separator, pos ? pos + strlen(_separator) : 1, strlen(_string));
      tmpStr =  substr(_string, oldPos, pos ? pos - oldPos : strlen(_string) + 1 - oldPos);

      if (tmpStr || ! _ignoreNearSeparator)
      {
         con += tmpStr;
      }

      oldPos =  pos + strlen(_separator);
  }
  while (pos);

  return con;
}
demas