comparison src/strfns.cc @ 6250:ff5e6cf72bda

[project @ 2007-01-23 18:37:52 by jwe]
author jwe
date Tue, 23 Jan 2007 18:37:52 +0000
parents a65b51ed388c
children 83949ae13b2c
comparison
equal deleted inserted replaced
6249:c507206c45bc 6250:ff5e6cf72bda
236 boolNDArray output (cell.dimensions); 236 boolNDArray output (cell.dimensions);
237 237
238 std::string s = r == 0 ? std::string () : str[0]; 238 std::string s = r == 0 ? std::string () : str[0];
239 239
240 for (int i = 0; i < cell.length (); i++) 240 for (int i = 0; i < cell.length (); i++)
241 if (cell(i).is_string ()) 241 {
242 output(i) = (cell(i).string_value () == s); 242 if (cell(i).is_string ())
243 else 243 output(i) = (cell(i).string_value () == s);
244 output(i) = false; 244 else
245 output(i) = false;
246 }
245 247
246 retval = output; 248 retval = output;
247 } 249 }
248 else if (r > 1) 250 else if (r > 1)
249 { 251 {
276 boolNDArray output (cell.dimensions); 278 boolNDArray output (cell.dimensions);
277 279
278 if (cell.length () == r) 280 if (cell.length () == r)
279 { 281 {
280 for (int i = 0; i < r; i++) 282 for (int i = 0; i < r; i++)
281 if (cell(i).is_string ()) 283 {
282 output(i) = (str[i] == cell(i).string_value ()); 284 if (cell(i).is_string ())
283 else 285 output(i) = (str[i] == cell(i).string_value ());
284 output(i) = false; 286 else
287 output(i) = false;
288 }
285 289
286 retval = output; 290 retval = output;
287 } 291 }
288 else 292 else
289 retval = false; 293 retval = false;
417 %!assert (all (strcmp ({'foo'}, y) == [false; false])); 421 %!assert (all (strcmp ({'foo'}, y) == [false; false]));
418 %!assert (all (strcmp (y, {'foo'}) == [false; false])); 422 %!assert (all (strcmp (y, {'foo'}) == [false; false]));
419 %!assert (all (strcmp (y, {'foo'}) == [false; false])); 423 %!assert (all (strcmp (y, {'foo'}) == [false; false]));
420 */ 424 */
421 425
426 DEFUN (strncmp, args, ,
427 "-*- texinfo -*-\n\
428 @deftypefn {Function File} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\
429 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are the same,\n\
430 and 0 otherwise.\n\
431 \n\
432 @example\n\
433 @group\n\
434 strncmp (\"abce\", \"abcd\", 3)\n\
435 @result{} 1\n\
436 @end group\n\
437 @end example\n\
438 \n\
439 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
440 of the same size is returned, containing the values described above for\n\
441 every member of the cell array. The other argument may also be a cell\n\
442 array of strings (of the same size or with only one element), char matrix\n\
443 or character string.\n\
444 \n\
445 @example\n\
446 @group\n\
447 strncmp (\"abce\", {\"abcd\", \"bca\", \"abc\"}, 3)\n\
448 @result{} [1, 0, 1]\n\
449 @end group\n\
450 @end example\n\
451 \n\
452 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp\n\
453 function returns 1 if the character strings are equal, and 0 otherwise.\n\
454 This is just the opposite of the corresponding C library function.\n\
455 @seealso{strncmpi, strcmp, strcmpi}\n\
456 @end deftypefn")
457 {
458 octave_value retval;
459
460 if (args.length () == 3)
461 {
462 bool s1_string = args(0).is_string ();
463 bool s1_cell = args(0).is_cell ();
464 bool s2_string = args(1).is_string ();
465 bool s2_cell = args(1).is_cell ();
466
467 // Match only first n strings.
468 int n = args(2).int_value ();
469
470 if (n <= 0)
471 {
472 error ("strncmp: N must be greater than 0");
473 return retval;
474 }
475
476 if (s1_string && s2_string)
477 {
478 // The only restriction here is that each string has equal or
479 // greater than n characters
480
481 const dim_vector dv1 = args(0).dims ();
482 const dim_vector dv2 = args(1).dims ();
483
484 if (dv1.numel () >= n && dv2.numel () >= n)
485 {
486 // Follow Matlab in the sense that the first n characters of
487 // the two strings (in column major order) need to be the same.
488 charNDArray s1 = args(0).char_array_value ();
489 charNDArray s2 = args(1).char_array_value ();
490
491 for (int i = 0; i < n; i++)
492 {
493 if (s1(i) != s2(i))
494 {
495 retval = false;
496 return retval;
497 }
498 }
499
500 retval = true;
501 }
502 else
503 retval = false;
504 }
505 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
506 {
507 string_vector str;
508 Cell cell;
509 int r, c;
510
511 if (s1_string)
512 {
513 str = args(0).all_strings ();
514 r = args(0).rows ();
515 c = args(0).columns ();
516 cell = args(1).cell_value ();
517 }
518 else
519 {
520 str = args(1).all_strings ();
521 r = args(1).rows ();
522 c = args(1).columns ();
523 cell = args(0).cell_value ();
524 }
525
526 if (r == 1)
527 {
528 // Broadcast the string.
529
530 boolNDArray output (cell.dimensions);
531
532 if (c < n)
533 {
534 for (int i = 0; i < cell.length (); i++)
535 output(i) = false;
536 }
537 else
538 {
539 for (int i = 0; i < cell.length (); i++)
540 {
541 if (cell(i).is_string ())
542 {
543 const std::string str2 = cell(i).string_value ();
544
545 if (str2.length() >= n)
546 {
547 if (str2.compare (0, n, str[0], 0, n) == 0)
548 output(i) = true;
549 else
550 output(i) = false;
551 }
552 else
553 output(i) = false;
554 }
555 }
556 }
557
558 retval = output;
559 }
560 else if (r > 1)
561 {
562 if (cell.length () == 1)
563 {
564 // Broadcast the cell.
565
566 const dim_vector dv (r, 1);
567 boolNDArray output (dv);
568
569 if (cell(0).is_string () && c >= n)
570 {
571 const std::string str2 = cell(0).string_value ();
572
573 if (str2.length () >= n)
574 {
575 for (int i = 0; i < r; i++)
576 {
577 if (str[i].compare (0, n, str2, 0, n) == 0)
578 output(i) = true;
579 else
580 output(i) = false;
581 }
582 }
583 else
584 {
585 for (int i = 0; i < r; i++)
586 output(i) = false;
587 }
588 }
589 else
590 {
591 for (int i = 0; i < r; i++)
592 output(i) = false;
593 }
594
595 retval = output;
596 }
597 else
598 {
599 // Must match in all dimensions.
600
601 boolNDArray output (cell.dimensions);
602
603 if (cell.numel () == r)
604 {
605 for (int i = 0; i < r; i++)
606 {
607 output(i) = false;
608
609 if (cell(i).is_string () && c >= n)
610 {
611 std::string str2 = cell(i).string_value ();
612
613 if (str2.length () >= n
614 && str2.compare (0, n, str[i], 0, n) == 0)
615 output(i) = true;
616 }
617 }
618
619 retval = output;
620 }
621 else
622 {
623 error ("strncmp: the number of rows of the string matrix must match the number of elements in the cell");
624 return retval;
625 }
626 }
627 }
628 }
629 else if (s1_cell && s2_cell)
630 {
631 Cell cell1;
632 Cell cell2;
633
634 int r1 = args(0).numel ();
635 int r2;
636
637 if (r1 == 1)
638 {
639 // Make the singleton cell2.
640
641 cell1 = args(1).cell_value ();
642 cell2 = args(0).cell_value ();
643 r1 = cell1.length ();
644 r2 = 1;
645 }
646 else
647 {
648 cell1 = args(0).cell_value ();
649 cell2 = args(1).cell_value ();
650 r2 = cell2.length ();
651 }
652
653 const dim_vector size1 = cell1.dimensions;
654 const dim_vector size2 = cell2.dimensions;
655
656 boolNDArray output (size1);
657
658 if (r2 == 1)
659 {
660 // Broadcast cell2.
661
662 if (! cell2(0).is_string ())
663 {
664 for (int i = 0; i < r1; i++)
665 output(i) = false;
666 }
667 else
668 {
669 const std::string str2 = cell2(0).string_value ();
670
671 for (int i = 0; i < r1; i++)
672 {
673 if (cell1(i).is_string ())
674 {
675 const std::string str1 = cell1(i).string_value ();
676
677 if (str1.length () >= n && str2.length () >= n
678 && str1.compare (0, n, str2, 0, n) == 0)
679 output(i) = true;
680 else
681 output(i) = false;
682 }
683 else
684 output(i) = false;
685 }
686 }
687 }
688 else
689 {
690 if (size1 != size2)
691 {
692 error ("strncmp: nonconformant cell arrays");
693 return retval;
694 }
695
696 for (int i = 0; i < r1; i++)
697 {
698 if (cell1(i).is_string () && cell2(i).is_string ())
699 {
700 const std::string str1 = cell1(i).string_value ();
701 const std::string str2 = cell2(i).string_value ();
702
703 if (str1.length () >= n && str2.length () >= n
704 && str1.compare (0, n, str2, 0, n) == 0)
705 output(i) = true;
706 else
707 output(i) = false;
708 }
709 else
710 output(i) = false;
711 }
712 }
713
714 retval = output;
715 }
716 else
717 retval = false;
718 }
719 else
720 print_usage ();
721
722 return retval;
723 }
724
422 DEFUN (list_in_columns, args, , 725 DEFUN (list_in_columns, args, ,
423 "-*- texinfo -*-\n\ 726 "-*- texinfo -*-\n\
424 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ 727 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\
425 Return a string containing the elements of @var{arg} listed in\n\ 728 Return a string containing the elements of @var{arg} listed in\n\
426 columns with an overall maximum width of @var{width}. The argument\n\ 729 columns with an overall maximum width of @var{width}. The argument\n\