LCOV - code coverage report
Current view: top level - src/utils/common - StringTokenizer.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 96.8 % 95 92
Test Date: 2026-05-24 16:29:35 Functions: 100.0 % 15 15

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    StringTokenizer.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @date    ?
      19              : ///
      20              : // A java-style StringTokenizer for c++ (stl)
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include <string>
      25              : #include <vector>
      26              : #include <iostream> // !!! debug only
      27              : 
      28              : #include "UtilExceptions.h"
      29              : #include "StringTokenizer.h"
      30              : 
      31              : 
      32              : // ===========================================================================
      33              : // variable definitions
      34              : // ===========================================================================
      35              : const int StringTokenizer::NEWLINE = -256;
      36              : const int StringTokenizer::WHITECHARS = -257;
      37              : const int StringTokenizer::SPACE = 32;
      38              : const int StringTokenizer::TAB = 9;
      39              : 
      40              : 
      41              : // ===========================================================================
      42              : // method definitions
      43              : // ===========================================================================
      44              : 
      45          120 : StringTokenizer::StringTokenizer() :
      46          120 :     myPos(0) {
      47          120 : }
      48              : 
      49              : 
      50      5364368 : StringTokenizer::StringTokenizer(std::string tosplit) :
      51      5364368 :     myTosplit(tosplit), myPos(0) {
      52      5364368 :     prepareWhitechar(tosplit);
      53      5364368 : }
      54              : 
      55              : 
      56     17437365 : StringTokenizer::StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars) :
      57     17437365 :     myTosplit(tosplit), myPos(0) {
      58     17437365 :     prepare(tosplit, token, splitAtAllChars);
      59     17437365 : }
      60              : 
      61              : 
      62         2531 : StringTokenizer::StringTokenizer(std::string tosplit, int special) :
      63         2531 :     myTosplit(tosplit), myPos(0) {
      64         2531 :     switch (special) {
      65              :         case NEWLINE:
      66            1 :             prepare(tosplit, "\r\n", true);
      67            1 :             break;
      68              :         case TAB:
      69          679 :             prepare(tosplit, "\t", true);
      70          679 :             break;
      71         1321 :         case WHITECHARS:
      72         1321 :             prepareWhitechar(tosplit);
      73              :             break;
      74          530 :         default:
      75          530 :             char* buf = new char[2];
      76          530 :             buf[0] = (char) special;
      77          530 :             buf[1] = 0;
      78          530 :             prepare(tosplit, buf, false);
      79          530 :             delete[] buf;
      80              :             break;
      81              :     }
      82         2531 : }
      83              : 
      84              : 
      85     22804384 : StringTokenizer::~StringTokenizer() {}
      86              : 
      87              : 
      88      3369272 : void StringTokenizer::reinit() {
      89      3369272 :     myPos = 0;
      90      3369272 : }
      91              : 
      92              : 
      93     37419893 : bool StringTokenizer::hasNext() {
      94     37419893 :     return myPos != (int)myStarts.size();
      95              : }
      96              : 
      97              : 
      98     52526317 : std::string StringTokenizer::next() {
      99     52526317 :     if (myPos >= (int)myStarts.size()) {
     100            8 :         throw OutOfBoundsException();
     101              :     }
     102     52526313 :     if (myLengths[myPos] == 0) {
     103           85 :         myPos++;
     104           85 :         return "";
     105              :     }
     106     52526228 :     int start = myStarts[myPos];
     107     52526228 :     int length = myLengths[myPos++];
     108     52526228 :     return myTosplit.substr(start, length);
     109              : }
     110              : 
     111              : 
     112          952 : std::string StringTokenizer::front() {
     113          952 :     if (myStarts.size() == 0) {
     114            0 :         throw OutOfBoundsException();
     115              :     }
     116          952 :     if (myLengths[0] == 0) {
     117            0 :         return "";
     118              :     }
     119          952 :     return myTosplit.substr(myStarts[0], myLengths[0]);
     120              : }
     121              : 
     122              : 
     123       281633 : std::string StringTokenizer::get(int pos) const {
     124       281633 :     if (pos >= (int)myStarts.size()) {
     125            4 :         throw OutOfBoundsException();
     126              :     }
     127       281631 :     if (myLengths[pos] == 0) {
     128          663 :         return "";
     129              :     }
     130       280968 :     int start = myStarts[pos];
     131              :     int length = myLengths[pos];
     132       280968 :     return myTosplit.substr(start, length);
     133              : }
     134              : 
     135              : 
     136     27788229 : int StringTokenizer::size() const {
     137     27788229 :     return (int)myStarts.size();
     138              : }
     139              : 
     140              : 
     141     17438575 : void StringTokenizer::prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars) {
     142     17438575 :     int beg = 0;
     143     17438575 :     int len = (int)token.length();
     144     17438575 :     if (splitAtAllChars) {
     145              :         len = 1;
     146              :     }
     147     55142477 :     while (beg < (int)tosplit.length()) {
     148              :         std::string::size_type end;
     149     37703902 :         if (splitAtAllChars) {
     150        14200 :             end = tosplit.find_first_of(token, beg);
     151              :         } else {
     152     37689702 :             end = tosplit.find(token, beg);
     153              :         }
     154     37703902 :         if (end == std::string::npos) {
     155              :             end = tosplit.length();
     156              :         }
     157     37703902 :         myStarts.push_back(beg);
     158     37703902 :         myLengths.push_back((int)end - beg);
     159     37703902 :         beg = (int)end + len;
     160     37703902 :         if (beg == (int)tosplit.length()) {
     161           45 :             myStarts.push_back(beg - 1);
     162           45 :             myLengths.push_back(0);
     163              :         }
     164              :     }
     165     17438575 : }
     166              : 
     167              : 
     168      5365689 : void StringTokenizer::prepareWhitechar(const std::string& tosplit) {
     169              :     std::string::size_type len = tosplit.length();
     170              :     std::string::size_type beg = 0;
     171      5365783 :     while (beg < len && (unsigned char)tosplit[beg] <= SPACE) {
     172           94 :         beg++;
     173              :     }
     174     21746321 :     while (beg != std::string::npos && beg < len) {
     175              :         std::string::size_type end = beg;
     176    187273698 :         while (end < len && (unsigned char)tosplit[end] > SPACE) {
     177    170893066 :             end++;
     178              :         }
     179     16380632 :         myStarts.push_back((int)beg);
     180     16380632 :         myLengths.push_back((int)end - (int)beg);
     181              :         beg = end;
     182     28103195 :         while (beg < len && (unsigned char)tosplit[beg] <= SPACE) {
     183     11722563 :             beg++;
     184              :         }
     185              :     }
     186      5365689 : }
     187              : 
     188              : 
     189              : std::vector<std::string>
     190      3369271 : StringTokenizer::getVector() {
     191              :     std::vector<std::string> ret;
     192      3369271 :     ret.reserve(size());
     193     11128603 :     while (hasNext()) {
     194     15518664 :         ret.push_back(next());
     195              :     }
     196      3369271 :     reinit();
     197      3369271 :     return ret;
     198            0 : }
     199              : 
     200              : 
     201              : std::set<std::string>
     202           30 : StringTokenizer::getSet() {
     203           30 :     std::vector<std::string> v = getVector();
     204           60 :     return std::set<std::string>(v.begin(), v.end());
     205           30 : }
     206              : 
     207              : 
     208              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1