Mercurial > octave-nkf
comparison libinterp/corefcn/regexp.cc @ 20590:1a0a433c8263
eliminate more simple uses of error_state
* dirfns.cc, oct-map.cc, oct-stream.cc, regexp.cc, ov-base-mat.cc,
ov-cell.cc, pt-idx.cc, pt-mat.cc:
Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 05 Oct 2015 21:13:12 -0400 |
parents | b6a59cc96bfa |
children | fd0efcdb3718 |
comparison
equal
deleted
inserted
replaced
20589:b10432a40432 | 20590:1a0a433c8263 |
---|---|
341 | 341 |
342 int nargin = args.length (); | 342 int nargin = args.length (); |
343 | 343 |
344 // Make sure we have string, pattern | 344 // Make sure we have string, pattern |
345 const std::string buffer = args(0).string_value (); | 345 const std::string buffer = args(0).string_value (); |
346 if (error_state) | |
347 return retval; | |
348 | 346 |
349 std::string pattern = args(1).string_value (); | 347 std::string pattern = args(1).string_value (); |
350 if (error_state) | 348 |
351 return retval; | |
352 // Matlab compatibility. | 349 // Matlab compatibility. |
353 if (args(1).is_sq_string ()) | 350 if (args(1).is_sq_string ()) |
354 pattern = do_regexp_ptn_string_escapes (pattern); | 351 pattern = do_regexp_ptn_string_escapes (pattern); |
355 | 352 |
356 regexp::opts options; | 353 regexp::opts options; |
357 options.case_insensitive (case_insensitive); | 354 options.case_insensitive (case_insensitive); |
358 bool extra_options = false; | 355 bool extra_options = false; |
359 parse_options (options, args, who, 2, extra_options); | 356 parse_options (options, args, who, 2, extra_options); |
360 if (error_state) | |
361 return retval; | |
362 | 357 |
363 regexp::match_data rx_lst = regexp_match (pattern, buffer, options, who); | 358 regexp::match_data rx_lst = regexp_match (pattern, buffer, options, who); |
364 | 359 |
365 string_vector named_pats = rx_lst.named_patterns (); | 360 string_vector named_pats = rx_lst.named_patterns (); |
366 | 361 |
367 size_t sz = rx_lst.size (); | 362 size_t sz = rx_lst.size (); |
368 | 363 |
369 if (! error_state) | 364 // Converted the linked list in the correct form for the return values |
365 | |
366 octave_idx_type i = 0; | |
367 octave_scalar_map nmap; | |
368 | |
369 retval.resize (7); | |
370 | |
371 if (sz == 1) | |
370 { | 372 { |
371 // Converted the linked list in the correct form for the return values | 373 string_vector named_tokens = rx_lst.begin ()->named_tokens (); |
372 | 374 |
373 octave_idx_type i = 0; | 375 for (int j = 0; j < named_pats.numel (); j++) |
374 octave_scalar_map nmap; | 376 nmap.assign (named_pats(j), named_tokens(j)); |
375 | 377 |
376 retval.resize (7); | 378 retval(5) = nmap; |
377 | 379 } |
378 if (sz == 1) | 380 else |
379 { | 381 { |
380 string_vector named_tokens = rx_lst.begin ()->named_tokens (); | 382 for (int j = 0; j < named_pats.numel (); j++) |
381 | 383 { |
382 for (int j = 0; j < named_pats.numel (); j++) | 384 Cell tmp (dim_vector (1, sz)); |
383 nmap.assign (named_pats(j), named_tokens(j)); | |
384 | |
385 retval(5) = nmap; | |
386 } | |
387 else | |
388 { | |
389 for (int j = 0; j < named_pats.numel (); j++) | |
390 { | |
391 Cell tmp (dim_vector (1, sz)); | |
392 | |
393 i = 0; | |
394 for (regexp::match_data::const_iterator p = rx_lst.begin (); | |
395 p != rx_lst.end (); p++) | |
396 { | |
397 string_vector named_tokens = p->named_tokens (); | |
398 | |
399 tmp(i++) = named_tokens(j); | |
400 } | |
401 | |
402 nmap.assign (named_pats(j), octave_value (tmp)); | |
403 } | |
404 | |
405 retval(5) = nmap; | |
406 } | |
407 | |
408 if (options.once ()) | |
409 { | |
410 regexp::match_data::const_iterator p = rx_lst.begin (); | |
411 | |
412 retval(4) = sz ? p->tokens () : Cell (); | |
413 retval(3) = sz ? p->match_string () : std::string (); | |
414 retval(2) = sz ? p->token_extents () : Matrix (); | |
415 | |
416 if (sz) | |
417 { | |
418 double start = p->start (); | |
419 double end = p->end (); | |
420 | |
421 Cell split (dim_vector (1, 2)); | |
422 split(0) = buffer.substr (0, start-1); | |
423 split(1) = buffer.substr (end); | |
424 | |
425 retval(6) = split; | |
426 retval(1) = end; | |
427 retval(0) = start; | |
428 } | |
429 else | |
430 { | |
431 retval(6) = buffer; | |
432 retval(1) = Matrix (); | |
433 retval(0) = Matrix (); | |
434 } | |
435 } | |
436 else | |
437 { | |
438 Cell tokens (dim_vector (1, sz)); | |
439 Cell match_string (dim_vector (1, sz)); | |
440 Cell token_extents (dim_vector (1, sz)); | |
441 NDArray end (dim_vector (1, sz)); | |
442 NDArray start (dim_vector (1, sz)); | |
443 Cell split (dim_vector (1, sz+1)); | |
444 size_t sp_start = 0; | |
445 | 385 |
446 i = 0; | 386 i = 0; |
447 for (regexp::match_data::const_iterator p = rx_lst.begin (); | 387 for (regexp::match_data::const_iterator p = rx_lst.begin (); |
448 p != rx_lst.end (); p++) | 388 p != rx_lst.end (); p++) |
449 { | 389 { |
450 double s = p->start (); | 390 string_vector named_tokens = p->named_tokens (); |
451 double e = p->end (); | 391 |
452 | 392 tmp(i++) = named_tokens(j); |
453 string_vector tmp = p->tokens (); | |
454 tokens(i) = Cell (dim_vector (1, tmp.numel ()), tmp); | |
455 match_string(i) = p->match_string (); | |
456 token_extents(i) = p->token_extents (); | |
457 end(i) = e; | |
458 start(i) = s; | |
459 split(i) = buffer.substr (sp_start, s-sp_start-1); | |
460 sp_start = e; | |
461 i++; | |
462 } | 393 } |
463 | 394 |
464 split(i) = buffer.substr (sp_start); | 395 nmap.assign (named_pats(j), octave_value (tmp)); |
396 } | |
397 | |
398 retval(5) = nmap; | |
399 } | |
400 | |
401 if (options.once ()) | |
402 { | |
403 regexp::match_data::const_iterator p = rx_lst.begin (); | |
404 | |
405 retval(4) = sz ? p->tokens () : Cell (); | |
406 retval(3) = sz ? p->match_string () : std::string (); | |
407 retval(2) = sz ? p->token_extents () : Matrix (); | |
408 | |
409 if (sz) | |
410 { | |
411 double start = p->start (); | |
412 double end = p->end (); | |
413 | |
414 Cell split (dim_vector (1, 2)); | |
415 split(0) = buffer.substr (0, start-1); | |
416 split(1) = buffer.substr (end); | |
465 | 417 |
466 retval(6) = split; | 418 retval(6) = split; |
467 retval(4) = tokens; | |
468 retval(3) = match_string; | |
469 retval(2) = token_extents; | |
470 retval(1) = end; | 419 retval(1) = end; |
471 retval(0) = start; | 420 retval(0) = start; |
472 } | 421 } |
473 | 422 else |
474 // Alter the order of the output arguments | 423 { |
475 | 424 retval(6) = buffer; |
476 if (extra_options) | 425 retval(1) = Matrix (); |
477 { | 426 retval(0) = Matrix (); |
478 int n = 0; | 427 } |
479 octave_value_list new_retval; | 428 } |
480 new_retval.resize (nargout); | 429 else |
481 | 430 { |
482 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); | 431 Cell tokens (dim_vector (1, sz)); |
432 Cell match_string (dim_vector (1, sz)); | |
433 Cell token_extents (dim_vector (1, sz)); | |
434 NDArray end (dim_vector (1, sz)); | |
435 NDArray start (dim_vector (1, sz)); | |
436 Cell split (dim_vector (1, sz+1)); | |
437 size_t sp_start = 0; | |
438 | |
439 i = 0; | |
440 for (regexp::match_data::const_iterator p = rx_lst.begin (); | |
441 p != rx_lst.end (); p++) | |
442 { | |
443 double s = p->start (); | |
444 double e = p->end (); | |
445 | |
446 string_vector tmp = p->tokens (); | |
447 tokens(i) = Cell (dim_vector (1, tmp.numel ()), tmp); | |
448 match_string(i) = p->match_string (); | |
449 token_extents(i) = p->token_extents (); | |
450 end(i) = e; | |
451 start(i) = s; | |
452 split(i) = buffer.substr (sp_start, s-sp_start-1); | |
453 sp_start = e; | |
454 i++; | |
455 } | |
456 | |
457 split(i) = buffer.substr (sp_start); | |
458 | |
459 retval(6) = split; | |
460 retval(4) = tokens; | |
461 retval(3) = match_string; | |
462 retval(2) = token_extents; | |
463 retval(1) = end; | |
464 retval(0) = start; | |
465 } | |
466 | |
467 // Alter the order of the output arguments | |
468 | |
469 if (extra_options) | |
470 { | |
471 int n = 0; | |
472 octave_value_list new_retval; | |
473 new_retval.resize (nargout); | |
474 | |
475 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); | |
476 for (int j = 0; j < 6; j++) | |
477 arg_used[j] = false; | |
478 | |
479 for (int j = 2; j < nargin; j++) | |
480 { | |
481 int k = 0; | |
482 std::string str = args(j).string_value (); | |
483 std::transform (str.begin (), str.end (), str.begin (), tolower); | |
484 | |
485 if (str.find ("once", 0) == 0 | |
486 || str.find ("stringanchors", 0) == 0 | |
487 || str.find ("lineanchors", 0) == 0 | |
488 || str.find ("matchcase", 0) == 0 | |
489 || str.find ("ignorecase", 0) == 0 | |
490 || str.find ("dotall", 0) == 0 | |
491 || str.find ("dotexceptnewline", 0) == 0 | |
492 || str.find ("literalspacing", 0) == 0 | |
493 || str.find ("freespacing", 0) == 0 | |
494 || str.find ("noemptymatch", 0) == 0 | |
495 || str.find ("emptymatch", 0) == 0) | |
496 continue; | |
497 else if (str.find ("start", 0) == 0) | |
498 k = 0; | |
499 else if (str.find ("end", 0) == 0) | |
500 k = 1; | |
501 else if (str.find ("tokenextents", 0) == 0) | |
502 k = 2; | |
503 else if (str.find ("match", 0) == 0) | |
504 k = 3; | |
505 else if (str.find ("tokens", 0) == 0) | |
506 k = 4; | |
507 else if (str.find ("names", 0) == 0) | |
508 k = 5; | |
509 else if (str.find ("split", 0) == 0) | |
510 k = 6; | |
511 | |
512 new_retval(n++) = retval(k); | |
513 arg_used[k] = true; | |
514 | |
515 if (n == nargout) | |
516 break; | |
517 } | |
518 | |
519 // Fill in the rest of the arguments | |
520 if (n < nargout) | |
521 { | |
483 for (int j = 0; j < 6; j++) | 522 for (int j = 0; j < 6; j++) |
484 arg_used[j] = false; | |
485 | |
486 for (int j = 2; j < nargin; j++) | |
487 { | 523 { |
488 int k = 0; | 524 if (! arg_used[j]) |
489 std::string str = args(j).string_value (); | 525 new_retval(n++) = retval(j); |
490 std::transform (str.begin (), str.end (), str.begin (), tolower); | |
491 | |
492 if (str.find ("once", 0) == 0 | |
493 || str.find ("stringanchors", 0) == 0 | |
494 || str.find ("lineanchors", 0) == 0 | |
495 || str.find ("matchcase", 0) == 0 | |
496 || str.find ("ignorecase", 0) == 0 | |
497 || str.find ("dotall", 0) == 0 | |
498 || str.find ("dotexceptnewline", 0) == 0 | |
499 || str.find ("literalspacing", 0) == 0 | |
500 || str.find ("freespacing", 0) == 0 | |
501 || str.find ("noemptymatch", 0) == 0 | |
502 || str.find ("emptymatch", 0) == 0) | |
503 continue; | |
504 else if (str.find ("start", 0) == 0) | |
505 k = 0; | |
506 else if (str.find ("end", 0) == 0) | |
507 k = 1; | |
508 else if (str.find ("tokenextents", 0) == 0) | |
509 k = 2; | |
510 else if (str.find ("match", 0) == 0) | |
511 k = 3; | |
512 else if (str.find ("tokens", 0) == 0) | |
513 k = 4; | |
514 else if (str.find ("names", 0) == 0) | |
515 k = 5; | |
516 else if (str.find ("split", 0) == 0) | |
517 k = 6; | |
518 | |
519 new_retval(n++) = retval(k); | |
520 arg_used[k] = true; | |
521 | |
522 if (n == nargout) | |
523 break; | |
524 } | 526 } |
525 | 527 } |
526 // Fill in the rest of the arguments | 528 |
527 if (n < nargout) | 529 retval = new_retval; |
528 { | |
529 for (int j = 0; j < 6; j++) | |
530 { | |
531 if (! arg_used[j]) | |
532 new_retval(n++) = retval(j); | |
533 } | |
534 } | |
535 | |
536 retval = new_retval; | |
537 } | |
538 } | 530 } |
539 | 531 |
540 return retval; | 532 return retval; |
541 } | 533 } |
542 | 534 |
565 for (octave_idx_type i = 0; i < cellstr.numel (); i++) | 557 for (octave_idx_type i = 0; i < cellstr.numel (); i++) |
566 { | 558 { |
567 new_args(0) = cellstr(i); | 559 new_args(0) = cellstr(i); |
568 octave_value_list tmp = octregexp (new_args, nargout, who, | 560 octave_value_list tmp = octregexp (new_args, nargout, who, |
569 case_insensitive); | 561 case_insensitive); |
570 | |
571 if (error_state) | |
572 break; | |
573 | 562 |
574 for (int j = 0; j < nargout; j++) | 563 for (int j = 0; j < nargout; j++) |
575 newretval[j](i) = tmp(j); | 564 newretval[j](i) = tmp(j); |
576 } | 565 } |
577 } | 566 } |
586 { | 575 { |
587 new_args(1) = cellpat(i); | 576 new_args(1) = cellpat(i); |
588 octave_value_list tmp = octregexp (new_args, nargout, who, | 577 octave_value_list tmp = octregexp (new_args, nargout, who, |
589 case_insensitive); | 578 case_insensitive); |
590 | 579 |
591 if (error_state) | |
592 break; | |
593 | |
594 for (int j = 0; j < nargout; j++) | 580 for (int j = 0; j < nargout; j++) |
595 newretval[j](i) = tmp(j); | 581 newretval[j](i) = tmp(j); |
596 } | 582 } |
597 } | 583 } |
598 else if (cellstr.numel () == cellpat.numel ()) | 584 else if (cellstr.numel () == cellpat.numel ()) |
610 new_args(0) = cellstr(i); | 596 new_args(0) = cellstr(i); |
611 new_args(1) = cellpat(i); | 597 new_args(1) = cellpat(i); |
612 | 598 |
613 octave_value_list tmp = octregexp (new_args, nargout, who, | 599 octave_value_list tmp = octregexp (new_args, nargout, who, |
614 case_insensitive); | 600 case_insensitive); |
615 | |
616 if (error_state) | |
617 break; | |
618 | 601 |
619 for (int j = 0; j < nargout; j++) | 602 for (int j = 0; j < nargout; j++) |
620 newretval[j](i) = tmp(j); | 603 newretval[j](i) = tmp(j); |
621 } | 604 } |
622 } | 605 } |
633 { | 616 { |
634 new_args(0) = cellstr(i); | 617 new_args(0) = cellstr(i); |
635 octave_value_list tmp = octregexp (new_args, nargout, who, | 618 octave_value_list tmp = octregexp (new_args, nargout, who, |
636 case_insensitive); | 619 case_insensitive); |
637 | 620 |
638 if (error_state) | |
639 break; | |
640 | |
641 for (int j = 0; j < nargout; j++) | 621 for (int j = 0; j < nargout; j++) |
642 newretval[j](i) = tmp(j); | 622 newretval[j](i) = tmp(j); |
643 } | 623 } |
644 } | 624 } |
645 | 625 |
646 if (!error_state) | 626 for (int j = 0; j < nargout; j++) |
647 for (int j = 0; j < nargout; j++) | 627 retval(j) = octave_value (newretval[j]); |
648 retval(j) = octave_value (newretval[j]); | |
649 } | 628 } |
650 else if (args(1).is_cell ()) | 629 else if (args(1).is_cell ()) |
651 { | 630 { |
652 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); | 631 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); |
653 octave_value_list new_args = args; | 632 octave_value_list new_args = args; |
660 { | 639 { |
661 new_args(1) = cellpat(i); | 640 new_args(1) = cellpat(i); |
662 octave_value_list tmp = octregexp (new_args, nargout, who, | 641 octave_value_list tmp = octregexp (new_args, nargout, who, |
663 case_insensitive); | 642 case_insensitive); |
664 | 643 |
665 if (error_state) | |
666 break; | |
667 | |
668 for (int j = 0; j < nargout; j++) | 644 for (int j = 0; j < nargout; j++) |
669 newretval[j](i) = tmp(j); | 645 newretval[j](i) = tmp(j); |
670 } | 646 } |
671 | 647 |
672 if (!error_state) | 648 for (int j = 0; j < nargout; j++) |
673 { | 649 retval(j) = octave_value (newretval[j]); |
674 for (int j = 0; j < nargout; j++) | |
675 retval(j) = octave_value (newretval[j]); | |
676 } | |
677 } | 650 } |
678 else | 651 else |
679 retval = octregexp (args, nargout, who, case_insensitive); | 652 retval = octregexp (args, nargout, who, case_insensitive); |
680 | 653 |
681 return retval; | 654 return retval; |
1338 | 1311 |
1339 int nargin = args.length (); | 1312 int nargin = args.length (); |
1340 | 1313 |
1341 // Make sure we have string, pattern, replacement | 1314 // Make sure we have string, pattern, replacement |
1342 const std::string buffer = args(0).string_value (); | 1315 const std::string buffer = args(0).string_value (); |
1343 if (error_state) | |
1344 return retval; | |
1345 | 1316 |
1346 std::string pattern = args(1).string_value (); | 1317 std::string pattern = args(1).string_value (); |
1347 if (error_state) | 1318 |
1348 return retval; | |
1349 // Matlab compatibility. | 1319 // Matlab compatibility. |
1350 if (args(1).is_sq_string ()) | 1320 if (args(1).is_sq_string ()) |
1351 pattern = do_regexp_ptn_string_escapes (pattern); | 1321 pattern = do_regexp_ptn_string_escapes (pattern); |
1352 | 1322 |
1353 std::string replacement = args(2).string_value (); | 1323 std::string replacement = args(2).string_value (); |
1354 if (error_state) | 1324 |
1355 return retval; | |
1356 // Matlab compatibility. | 1325 // Matlab compatibility. |
1357 if (args(2).is_sq_string ()) | 1326 if (args(2).is_sq_string ()) |
1358 replacement = do_regexp_rep_string_escapes (replacement); | 1327 replacement = do_regexp_rep_string_escapes (replacement); |
1359 | 1328 |
1360 // Pack options excluding 'tokenize' and various output | 1329 // Pack options excluding 'tokenize' and various output |
1375 regexpargs.resize (len); | 1344 regexpargs.resize (len); |
1376 | 1345 |
1377 regexp::opts options; | 1346 regexp::opts options; |
1378 bool extra_args = false; | 1347 bool extra_args = false; |
1379 parse_options (options, regexpargs, who, 0, extra_args); | 1348 parse_options (options, regexpargs, who, 0, extra_args); |
1380 if (error_state) | |
1381 return retval; | |
1382 | 1349 |
1383 return regexp_replace (pattern, buffer, replacement, options, who); | 1350 return regexp_replace (pattern, buffer, replacement, options, who); |
1384 } | 1351 } |
1385 | 1352 |
1386 DEFUN (regexprep, args, , | 1353 DEFUN (regexprep, args, , |
1463 error ("regexprep: inconsistent cell array dimensions"); | 1430 error ("regexprep: inconsistent cell array dimensions"); |
1464 } | 1431 } |
1465 else if (rep.numel () != 1) | 1432 else if (rep.numel () != 1) |
1466 dv1 = rep.dims (); | 1433 dv1 = rep.dims (); |
1467 | 1434 |
1468 if (!error_state) | 1435 Cell ret (dv0); |
1469 { | 1436 octave_value_list new_args = args; |
1470 Cell ret (dv0); | 1437 |
1471 octave_value_list new_args = args; | 1438 for (octave_idx_type i = 0; i < dv0.numel (); i++) |
1472 | 1439 { |
1473 for (octave_idx_type i = 0; i < dv0.numel (); i++) | 1440 new_args(0) = str(i); |
1441 if (pat.numel () == 1) | |
1442 new_args(1) = pat(0); | |
1443 if (rep.numel () == 1) | |
1444 new_args(2) = rep(0); | |
1445 | |
1446 for (octave_idx_type j = 0; j < dv1.numel (); j++) | |
1474 { | 1447 { |
1475 new_args(0) = str(i); | 1448 if (pat.numel () != 1) |
1476 if (pat.numel () == 1) | 1449 new_args(1) = pat(j); |
1477 new_args(1) = pat(0); | 1450 if (rep.numel () != 1) |
1478 if (rep.numel () == 1) | 1451 new_args(2) = rep(j); |
1479 new_args(2) = rep(0); | 1452 new_args(0) = octregexprep (new_args, "regexprep"); |
1480 | |
1481 for (octave_idx_type j = 0; j < dv1.numel (); j++) | |
1482 { | |
1483 if (pat.numel () != 1) | |
1484 new_args(1) = pat(j); | |
1485 if (rep.numel () != 1) | |
1486 new_args(2) = rep(j); | |
1487 new_args(0) = octregexprep (new_args, "regexprep"); | |
1488 | |
1489 if (error_state) | |
1490 break; | |
1491 } | |
1492 | |
1493 if (error_state) | |
1494 break; | |
1495 | |
1496 ret(i) = new_args(0); | |
1497 } | 1453 } |
1498 | 1454 |
1499 if (!error_state) | 1455 ret(i) = new_args(0); |
1500 retval = args(0).is_cell () ? octave_value (ret) | 1456 } |
1501 : octave_value (ret(0)); | 1457 |
1502 } | 1458 retval = args(0).is_cell () ? octave_value (ret) |
1459 : octave_value (ret(0)); | |
1503 } | 1460 } |
1504 else | 1461 else |
1505 retval = octregexprep (args, "regexprep"); | 1462 retval = octregexprep (args, "regexprep"); |
1506 | 1463 |
1507 return retval; | 1464 return retval; |