Re: [Vala] Is this a bug in VALA ?



It's not a bug in Vala, it's three bugs in your code.

You should only loop until <len and not all the way to <=len because strings chars are index from 0 up to 
length-1.
Also note that the second parameter to .substring() takes a "count to include in the substring" and not
"index in the source string where the substring should stop" and thus using len.substring(x, line.len()) is
overkill whenever x>0. Also you have switch the location of % and the formatter char in the two last
stdout lines. And you shouldn't use %f to print integers, use %d, %ld or similar depending on the bit length
of the integer.

Below is a fixed code sample that retains your "coding style".
Happy Vala hacking! :-)




public class Main
{
  public static int main (string[] args)
  {
      long index = 0;
      string col1,col2;
      string line = "happy doggy";
      for(;index<line.len();index++)
            if(line[index] == ' ') break;
      col1 = line.substring(0,index);
      col2 = line.substring(index+1,line.len() - index - 1);

      stdout.printf ("Line - %s\n", line);
      stdout.printf ("index - %ld ,Len() - %ld\n" ,index,line.len());
      stdout.printf ("Col1 - %s ,Col2 - %s\n" ,col1,col2);
    return 0;
  }
}

                Martin

Arkadi Viner wrote:
Hello.
I am trying to split a string into two strings.
The string should be split-ed by locating a first space character from the
left.
Please look at a simple code I made:

public class Main
{
  public static int main (string[] args)
  {
      long index = 0;
      string col1,col2;
      string line = "happy doggy";
      for(;index<=line.len();index++)
            if(line[index] == ' ') break;
      col1 = line.substring(0,index);
      col2 = line.substring(index+1,line.len());

      stdout.printf ("Line - %s\n", line);
      stdout.printf ("index - %f ,Len() - f%\n" ,index,line.len());
      stdout.printf ("Col1 - %s ,Col2 - s%\n" ,col1,col2);
    return 0;
  }
}

*When I compile this code I get:*

/home/arkadi/Bug/main.vala.c: In function ‘main_main’:
/home/arkadi/Bug/main.vala.c:113: warning: unknown conversion type character
0xa in format
/home/arkadi/Bug/main.vala.c:114: warning: unknown conversion type character
0xa in format
<b>Process return 0  execution time: 0.674968 s</b>

*When I run this code I get:*

Line - happy doggy
<b>Process return 0  execution time: 0.008026 s</b>
index - 5.000000 ,Len() - f%
** (process:7502): CRITICAL **: string_substring: assertion `(offset + len)
&lt;= string_length' failed
Col1 - happy ,Col2 - s%

You can see that, It finds the place where to split the string very well
(5.000000) and col1="happy"
,but for the second part, line.Len() = f% and col2 = s%...
I have tried to change line.len()   to numbers, but it's not the problem...
How to get col2="doggy" ???
Maybe some one know what is the problem with this code? or maybe it is
really a bug?



------------------------------------------------------------------------

_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]