comparison libinterp/corefcn/textscan.cc @ 21496:5d69326e8cc5

textscan: Prefer std::string local variables to char arrays * textscan.cc (textscan::lookahead, textscan::scan_complex, textscan::skip_whitespace): Use std::string for local buffers rather than char arrays.
author Mike Miller <mtmiller@octave.org>
date Fri, 18 Mar 2016 22:37:52 -0700
parents 8e9833b8791d
children 2d71bb0011a0
comparison
equal deleted inserted replaced
21495:82089c8ed7fa 21496:5d69326e8cc5
1747 is.seekg (pos); // reset to position before failed read 1747 is.seekg (pos); // reset to position before failed read
1748 1748
1749 // treat_as_empty strings may be different sizes. 1749 // treat_as_empty strings may be different sizes.
1750 // Read ahead longest, put it all back, then re-read the string 1750 // Read ahead longest, put it all back, then re-read the string
1751 // that matches. 1751 // that matches.
1752 char *look, look_buf [treat_as_empty_len + 1]; 1752 std::string look_buf (treat_as_empty_len, '\0');
1753 // prefill, in case EOF means part-filled. 1753 char *look = is.read (&look_buf[0], look_buf.size (), pos);
1754 memset (look_buf, '\0', treat_as_empty_len);
1755 look = is.read (look_buf, treat_as_empty_len, pos);
1756 1754
1757 is.clear (state); 1755 is.clear (state);
1758 is.seekg (pos); // reset to position before look-ahead 1756 is.seekg (pos); // reset to position before look-ahead
1759 // FIXME -- is.read could invalidate pos 1757 // FIXME -- is.read could invalidate pos
1760 1758
1763 std::string s = treat_as_empty (i).string_value (); 1761 std::string s = treat_as_empty (i).string_value ();
1764 if (! strncmp (s.c_str (), look, s.size ())) 1762 if (! strncmp (s.c_str (), look, s.size ()))
1765 { 1763 {
1766 as_empty = true; 1764 as_empty = true;
1767 // read just the right amount 1765 // read just the right amount
1768 is.read (look_buf, s.size (), pos); 1766 is.read (&look_buf[0], s.size (), pos);
1769 break; 1767 break;
1770 } 1768 }
1771 } 1769 }
1772 } 1770 }
1773 } 1771 }
2545 { 2543 {
2546 // save stream state in case we have to restore it 2544 // save stream state in case we have to restore it
2547 char *pos = is.tellg (); 2545 char *pos = is.tellg ();
2548 std::ios::iostate state = is.rdstate (); 2546 std::ios::iostate state = is.rdstate ();
2549 2547
2550 char *look, tmp [comment_len]; 2548 std::string tmp (comment_len, '\0');
2551 look = is.read (tmp, comment_len-1, pos); // already read first char 2549 char *look = is.read (&tmp[0], comment_len-1, pos); // already read first char
2552 if (is && ! strncmp (comment_style(0).string_value ().substr (1) 2550 if (is && ! strncmp (comment_style(0).string_value ().substr (1)
2553 .c_str (), look, comment_len-1)) 2551 .c_str (), look, comment_len-1))
2554 { 2552 {
2555 found_comment = true; 2553 found_comment = true;
2556 2554
2557 std::string dummy; 2555 std::string dummy;
2558 char eol [3] = {static_cast<char> (eol1),
2559 static_cast<char> (eol2),
2560 '\0'};
2561 if (comment_style.numel () == 1) // skip to end of line 2556 if (comment_style.numel () == 1) // skip to end of line
2562 { 2557 {
2558 std::string eol (3, '\0');
2559 eol[0] = eol1;
2560 eol[1] = eol2;
2561
2563 scan_caret (is, eol, dummy); 2562 scan_caret (is, eol, dummy);
2564 c1 = is.get_undelim (); 2563 c1 = is.get_undelim ();
2565 if (c1 == eol1 && eol1 != eol2 && is.peek_undelim () == eol2) 2564 if (c1 == eol1 && eol1 != eol2 && is.peek_undelim () == eol2)
2566 is.get_undelim (); 2565 is.get_undelim ();
2567 lines++; 2566 lines++;
2568 } 2567 }
2569 else // matching pair 2568 else // matching pair
2570 { 2569 {
2571 std::string end_c = comment_style(1).string_value (); 2570 std::string end_c = comment_style(1).string_value ();
2572 // last char of end-comment sequence 2571 // last char of end-comment sequence
2573 char last[2] = {*(end_c.substr (end_c.length ()-1).c_str ()), 2572 std::string last = end_c.substr (end_c.size () - 1);
2574 '\0'};
2575 std::string may_match (""); 2573 std::string may_match ("");
2576 do 2574 do
2577 { // find sequence ending with last char 2575 { // find sequence ending with last char
2578 scan_caret (is, last, dummy); 2576 scan_caret (is, last, dummy);
2579 is.get_undelim (); // (read last itself) 2577 is.get_undelim (); // (read last itself)
2580 2578
2581 may_match = may_match + dummy + *last; 2579 may_match = may_match + dummy + last;
2582 int start = may_match.length () - end_c.length (); 2580 int start = may_match.length () - end_c.length ();
2583 if (start < 0) 2581 if (start < 0)
2584 start = 0; 2582 start = 0;
2585 may_match = may_match.substr (start); 2583 may_match = may_match.substr (start);
2586 } 2584 }
2613 // Read ahead longest, put it all back, then re-read the string 2611 // Read ahead longest, put it all back, then re-read the string
2614 // that matches. 2612 // that matches.
2615 2613
2616 char *pos = is.tellg (); 2614 char *pos = is.tellg ();
2617 2615
2618 char *look, tmp [max_len + 1]; 2616 std::string tmp (max_len, '\0');
2619 2617 char *look = is.read (&tmp[0], tmp.size (), pos);
2620 memset (tmp, '\0', max_len); // prefill, in case EOF means part-filled.
2621 look = is.read (tmp, max_len, pos);
2622 2618
2623 is.clear (); 2619 is.clear ();
2624 is.seekg (pos); // reset to position before look-ahead 2620 is.seekg (pos); // reset to position before look-ahead
2625 // FIXME pos may be corrupted by is.read 2621 // FIXME pos may be corrupted by is.read
2626 2622
2631 for (i = 0; i < targets.numel (); i++) 2627 for (i = 0; i < targets.numel (); i++)
2632 { 2628 {
2633 std::string s = targets (i).string_value (); 2629 std::string s = targets (i).string_value ();
2634 if (! (*compare) (s.c_str (), look, s.size ())) 2630 if (! (*compare) (s.c_str (), look, s.size ()))
2635 { 2631 {
2636 is.read (tmp, s.size (), pos); // read just the right amount 2632 is.read (&tmp[0], s.size (), pos); // read just the right amount
2637 break; 2633 break;
2638 } 2634 }
2639 } 2635 }
2640 2636
2641 if (i == targets.numel ()) 2637 if (i == targets.numel ())