comparison src/strfns.cc @ 8809:732cb0236488

strfns.cc: use size_t or octave_idx_type instead of int as needed
author John W. Eaton <jwe@octave.org>
date Wed, 18 Feb 2009 15:05:06 -0500
parents 937921654627
children c3445f1c8cb4
comparison
equal deleted inserted replaced
8808:d724487d2c4b 8809:732cb0236488
190 190
191 if (nargin > 0) 191 if (nargin > 0)
192 { 192 {
193 int n_elts = 0; 193 int n_elts = 0;
194 194
195 int max_len = 0; 195 size_t max_len = 0;
196 196
197 std::queue<string_vector> args_as_strings; 197 std::queue<string_vector> args_as_strings;
198 198
199 for (int i = 0; i < nargin; i++) 199 for (int i = 0; i < nargin; i++)
200 { 200 {
204 { 204 {
205 error ("strvcat: unable to convert some args to strings"); 205 error ("strvcat: unable to convert some args to strings");
206 return retval; 206 return retval;
207 } 207 }
208 208
209 int n = s.length (); 209 size_t n = s.length ();
210 210
211 // do not count empty strings in calculation of number of elements 211 // do not count empty strings in calculation of number of elements
212 if (n > 0) 212 if (n > 0)
213 { 213 {
214 for (int j = 0; j < n; j++) 214 for (size_t j = 0; j < n; j++)
215 { 215 {
216 if (s[j].length () > 0) 216 if (s[j].length () > 0)
217 n_elts++; 217 n_elts++;
218 } 218 }
219 } 219 }
220 220
221 int s_max_len = s.max_length (); 221 size_t s_max_len = s.max_length ();
222 222
223 if (s_max_len > max_len) 223 if (s_max_len > max_len)
224 max_len = s_max_len; 224 max_len = s_max_len;
225 225
226 args_as_strings.push (s); 226 args_as_strings.push (s);
227 } 227 }
228 228
229 string_vector result (n_elts); 229 string_vector result (n_elts);
230 230
231 int k = 0; 231 octave_idx_type k = 0;
232 232
233 for (int i = 0; i < nargin; i++) 233 for (int i = 0; i < nargin; i++)
234 { 234 {
235 string_vector s = args_as_strings.front (); 235 string_vector s = args_as_strings.front ();
236 args_as_strings.pop (); 236 args_as_strings.pop ();
237 237
238 int n = s.length (); 238 size_t n = s.length ();
239 239
240 if (n > 0) 240 if (n > 0)
241 { 241 {
242 for (int j = 0; j < n; j++) 242 for (size_t j = 0; j < n; j++)
243 { 243 {
244 std::string t = s[j]; 244 std::string t = s[j];
245 if (t.length () > 0) 245 if (t.length () > 0)
246 { 246 {
247 int t_len = t.length (); 247 size_t t_len = t.length ();
248 248
249 if (max_len > t_len) 249 if (max_len > t_len)
250 t += std::string (max_len - t_len, ' '); 250 t += std::string (max_len - t_len, ' ');
251 251
252 result[k++] = t; 252 result[k++] = t;
341 const dim_vector dv1 = args(0).dims (); 341 const dim_vector dv1 = args(0).dims ();
342 const dim_vector dv2 = args(1).dims (); 342 const dim_vector dv2 = args(1).dims ();
343 343
344 if (dv1.length () == dv2.length ()) 344 if (dv1.length () == dv2.length ())
345 { 345 {
346 for (int i = 0; i < dv1.length (); i++) 346 for (octave_idx_type i = 0; i < dv1.length (); i++)
347 { 347 {
348 if (dv1(i) != dv2(i)) 348 if (dv1(i) != dv2(i))
349 { 349 {
350 retval = false; 350 retval = false;
351 return retval; 351 return retval;
357 else 357 else
358 { 358 {
359 charNDArray s1 = args(0).char_array_value (); 359 charNDArray s1 = args(0).char_array_value ();
360 charNDArray s2 = args(1).char_array_value (); 360 charNDArray s2 = args(1).char_array_value ();
361 361
362 for (int i = 0; i < dv1.numel (); i++) 362 for (octave_idx_type i = 0; i < dv1.numel (); i++)
363 { 363 {
364 if (s1(i) != s2(i)) 364 if (s1(i) != s2(i))
365 { 365 {
366 retval = false; 366 retval = false;
367 return retval; 367 return retval;
374 } 374 }
375 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) 375 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
376 { 376 {
377 string_vector str; 377 string_vector str;
378 Cell cell; 378 Cell cell;
379 int r; 379 octave_idx_type r;
380 380
381 if (s1_string) 381 if (s1_string)
382 { 382 {
383 str = args(0).all_strings (); 383 str = args(0).all_strings ();
384 r = args(0).rows (); 384 r = args(0).rows ();
397 397
398 boolNDArray output (cell.dims (), false); 398 boolNDArray output (cell.dims (), false);
399 399
400 std::string s = r == 0 ? std::string () : str[0]; 400 std::string s = r == 0 ? std::string () : str[0];
401 401
402 for (int i = 0; i < cell.length (); i++) 402 for (octave_idx_type i = 0; i < cell.length (); i++)
403 { 403 {
404 if (cell(i).is_string ()) 404 if (cell(i).is_string ())
405 output(i) = (cell(i).string_value () == s); 405 output(i) = (cell(i).string_value () == s);
406 } 406 }
407 407
418 418
419 if (cell(0).is_string ()) 419 if (cell(0).is_string ())
420 { 420 {
421 const std::string str2 = cell(0).string_value (); 421 const std::string str2 = cell(0).string_value ();
422 422
423 for (int i = 0; i < r; i++) 423 for (octave_idx_type i = 0; i < r; i++)
424 output(i) = (str[i] == str2); 424 output(i) = (str[i] == str2);
425 } 425 }
426 426
427 retval = output; 427 retval = output;
428 } 428 }
432 432
433 boolNDArray output (cell.dims (), false); 433 boolNDArray output (cell.dims (), false);
434 434
435 if (cell.length () == r) 435 if (cell.length () == r)
436 { 436 {
437 for (int i = 0; i < r; i++) 437 for (octave_idx_type i = 0; i < r; i++)
438 { 438 {
439 if (cell(i).is_string ()) 439 if (cell(i).is_string ())
440 output(i) = (str[i] == cell(i).string_value ()); 440 output(i) = (str[i] == cell(i).string_value ());
441 } 441 }
442 442
450 else if (s1_cell && s2_cell) 450 else if (s1_cell && s2_cell)
451 { 451 {
452 Cell cell1; 452 Cell cell1;
453 Cell cell2; 453 Cell cell2;
454 454
455 int r1 = args(0).numel (); 455 octave_idx_type r1 = args(0).numel ();
456 int r2; 456 octave_idx_type r2;
457 457
458 if (r1 == 1) 458 if (r1 == 1)
459 { 459 {
460 // Make the singleton cell2. 460 // Make the singleton cell2.
461 461
482 482
483 if (cell2(0).is_string ()) 483 if (cell2(0).is_string ())
484 { 484 {
485 const std::string str2 = cell2(0).string_value (); 485 const std::string str2 = cell2(0).string_value ();
486 486
487 for (int i = 0; i < r1; i++) 487 for (octave_idx_type i = 0; i < r1; i++)
488 { 488 {
489 if (cell1(i).is_string ()) 489 if (cell1(i).is_string ())
490 { 490 {
491 const std::string str1 = cell1(i).string_value (); 491 const std::string str1 = cell1(i).string_value ();
492 output(i) = (str1 == str2); 492 output(i) = (str1 == str2);
500 { 500 {
501 error ("strcmp: nonconformant cell arrays"); 501 error ("strcmp: nonconformant cell arrays");
502 return retval; 502 return retval;
503 } 503 }
504 504
505 for (int i = 0; i < r1; i++) 505 for (octave_idx_type i = 0; i < r1; i++)
506 { 506 {
507 if (cell1(i).is_string () && cell2(i).is_string ()) 507 if (cell1(i).is_string () && cell2(i).is_string ())
508 { 508 {
509 const std::string str1 = cell1(i).string_value (); 509 const std::string str1 = cell1(i).string_value ();
510 const std::string str2 = cell2(i).string_value (); 510 const std::string str2 = cell2(i).string_value ();
607 bool s1_cell = args(0).is_cell (); 607 bool s1_cell = args(0).is_cell ();
608 bool s2_string = args(1).is_string (); 608 bool s2_string = args(1).is_string ();
609 bool s2_cell = args(1).is_cell (); 609 bool s2_cell = args(1).is_cell ();
610 610
611 // Match only first n strings. 611 // Match only first n strings.
612 int n = args(2).int_value (); 612 int n = args(2).int_value ();
613 613
614 if (n <= 0) 614 if (n <= 0)
615 { 615 {
616 error ("strncmp: N must be greater than 0"); 616 error ("strncmp: N must be greater than 0");
617 return retval; 617 return retval;
648 } 648 }
649 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) 649 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
650 { 650 {
651 string_vector str; 651 string_vector str;
652 Cell cell; 652 Cell cell;
653 int r, c; 653 octave_idx_type r, c;
654 654
655 if (s1_string) 655 if (s1_string)
656 { 656 {
657 str = args(0).all_strings (); 657 str = args(0).all_strings ();
658 r = args(0).rows (); 658 r = args(0).rows ();
673 673
674 boolNDArray output (cell.dims (), false); 674 boolNDArray output (cell.dims (), false);
675 675
676 if (c < n) 676 if (c < n)
677 { 677 {
678 for (int i = 0; i < cell.length (); i++) 678 for (octave_idx_type i = 0; i < cell.length (); i++)
679 output(i) = false; 679 output(i) = false;
680 } 680 }
681 else 681 else
682 { 682 {
683 for (int i = 0; i < cell.length (); i++) 683 for (octave_idx_type i = 0; i < cell.length (); i++)
684 { 684 {
685 if (cell(i).is_string ()) 685 if (cell(i).is_string ())
686 { 686 {
687 const std::string str2 = cell(i).string_value (); 687 const std::string str2 = cell(i).string_value ();
688 688
689 if (str2.length() >= n 689 if (str2.length () >= n
690 && str2.compare (0, n, str[0], 0, n) == 0) 690 && str2.compare (0, n, str[0], 0, n) == 0)
691 output(i) = true; 691 output(i) = true;
692 } 692 }
693 } 693 }
694 } 694 }
708 { 708 {
709 const std::string str2 = cell(0).string_value (); 709 const std::string str2 = cell(0).string_value ();
710 710
711 if (str2.length () >= n) 711 if (str2.length () >= n)
712 { 712 {
713 for (int i = 0; i < r; i++) 713 for (octave_idx_type i = 0; i < r; i++)
714 { 714 {
715 if (str[i].compare (0, n, str2, 0, n) == 0) 715 if (str[i].compare (0, n, str2, 0, n) == 0)
716 output(i) = true; 716 output(i) = true;
717 } 717 }
718 } 718 }
726 726
727 boolNDArray output (cell.dims (), false); 727 boolNDArray output (cell.dims (), false);
728 728
729 if (cell.numel () == r) 729 if (cell.numel () == r)
730 { 730 {
731 for (int i = 0; i < r; i++) 731 for (octave_idx_type i = 0; i < r; i++)
732 { 732 {
733 if (cell(i).is_string () && c >= n) 733 if (cell(i).is_string () && c >= n)
734 { 734 {
735 std::string str2 = cell(i).string_value (); 735 std::string str2 = cell(i).string_value ();
736 736
753 else if (s1_cell && s2_cell) 753 else if (s1_cell && s2_cell)
754 { 754 {
755 Cell cell1; 755 Cell cell1;
756 Cell cell2; 756 Cell cell2;
757 757
758 int r1 = args(0).numel (); 758 octave_idx_type r1 = args(0).numel ();
759 int r2; 759 octave_idx_type r2;
760 760
761 if (r1 == 1) 761 if (r1 == 1)
762 { 762 {
763 // Make the singleton cell2. 763 // Make the singleton cell2.
764 764
785 785
786 if (cell2(0).is_string ()) 786 if (cell2(0).is_string ())
787 { 787 {
788 const std::string str2 = cell2(0).string_value (); 788 const std::string str2 = cell2(0).string_value ();
789 789
790 for (int i = 0; i < r1; i++) 790 for (octave_idx_type i = 0; i < r1; i++)
791 { 791 {
792 if (cell1(i).is_string ()) 792 if (cell1(i).is_string ())
793 { 793 {
794 const std::string str1 = cell1(i).string_value (); 794 const std::string str1 = cell1(i).string_value ();
795 795
806 { 806 {
807 error ("strncmp: nonconformant cell arrays"); 807 error ("strncmp: nonconformant cell arrays");
808 return retval; 808 return retval;
809 } 809 }
810 810
811 for (int i = 0; i < r1; i++) 811 for (octave_idx_type i = 0; i < r1; i++)
812 { 812 {
813 if (cell1(i).is_string () && cell2(i).is_string ()) 813 if (cell1(i).is_string () && cell2(i).is_string ())
814 { 814 {
815 const std::string str1 = cell1(i).string_value (); 815 const std::string str1 = cell1(i).string_value ();
816 const std::string str2 = cell2(i).string_value (); 816 const std::string str2 = cell2(i).string_value ();