LCOV - code coverage report
Current view: top level - src - strfnd.h (source / functions) Hit Total Coverage
Test: report Lines: 38 48 79.2 %
Date: 2015-07-11 18:23:49 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : 
       5             : This program is free software; you can redistribute it and/or modify
       6             : it under the terms of the GNU Lesser General Public License as published by
       7             : the Free Software Foundation; either version 2.1 of the License, or
       8             : (at your option) any later version.
       9             : 
      10             : This program is distributed in the hope that it will be useful,
      11             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : GNU Lesser General Public License for more details.
      14             : 
      15             : You should have received a copy of the GNU Lesser General Public License along
      16             : with this program; if not, write to the Free Software Foundation, Inc.,
      17             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             : */
      19             : 
      20             : #ifndef STRFND_HEADER
      21             : #define STRFND_HEADER
      22             : 
      23             : #include <string>
      24             : 
      25         541 : class Strfnd{
      26             :     std::string tek;
      27             :     unsigned int p;
      28             : public:
      29         541 :     void start(std::string niinq){
      30         541 :         tek = niinq;
      31         541 :         p=0;
      32         541 :     }
      33             :     unsigned int where(){
      34             :         return p;
      35             :     }
      36             :     void to(unsigned int i){
      37             :         p = i;
      38             :     }
      39             :     std::string what(){
      40             :         return tek;
      41             :     }
      42        3860 :     std::string next(std::string plop){
      43             :         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
      44             :         size_t n;
      45        3860 :         std::string palautus;
      46        3860 :         if (p < tek.size())
      47             :         {  
      48             :             //std::cout<<"\tp<tek.size()"<<std::endl;
      49        3815 :             if ((n = tek.find(plop, p)) == std::string::npos || plop == "")
      50             :             {  
      51             :                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
      52         533 :                 n = tek.size();
      53             :             }
      54             :             else
      55             :             {  
      56             :                 //std::cout<<"\t\tn != string::npos"<<std::endl;
      57             :             }
      58        3815 :             palautus = tek.substr(p, n-p);
      59        3815 :             p = n + plop.length();
      60             :         }
      61             :         //else
      62             :             //std::cout<<"\tp>=tek.size()"<<std::endl;
      63             :                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
      64        3860 :         return palautus;
      65             :     }
      66             :     
      67             :     // Returns substr of tek up to the next occurence of plop that isn't escaped with '\'
      68             :     std::string next_esc(std::string plop) {
      69             :                 size_t n, realp;
      70             :                 
      71             :         if (p >= tek.size())
      72             :                 return "";
      73             :                 
      74             :                 realp = p;
      75             :                 do {
      76             :                         n = tek.find(plop, p);
      77             :                         if (n == std::string::npos || plop == "")
      78             :                                 n = tek.length();
      79             :                         p = n + plop.length();
      80             :                 } while (n > 0 && tek[n - 1] == '\\');
      81             :                 
      82             :                 return tek.substr(realp, n - realp);
      83             :     }
      84             :     
      85           0 :         void skip_over(std::string chars){
      86           0 :                 while(p < tek.size()){
      87           0 :                         bool is = false;
      88           0 :                         for(unsigned int i=0; i<chars.size(); i++){
      89           0 :                                 if(chars[i] == tek[p]){
      90           0 :                                         is = true;
      91           0 :                                         break;
      92             :                                 }
      93             :                         }
      94           0 :                         if(!is) break;
      95           0 :                         p++;
      96             :                 }
      97           0 :         }
      98         756 :     bool atend(){
      99         756 :         if(p>=tek.size()) return true;
     100         735 :         return false;
     101             :     }
     102         541 :     Strfnd(std::string s){
     103         541 :         start(s);
     104         541 :     }
     105             : };
     106             : 
     107           4 : class WStrfnd{
     108             :     std::wstring tek;
     109             :     unsigned int p;
     110             : public:
     111           4 :     void start(std::wstring niinq){
     112           4 :         tek = niinq;
     113           4 :         p=0;
     114           4 :     }
     115             :     unsigned int where(){
     116             :         return p;
     117             :     }
     118             :     void to(unsigned int i){
     119             :         p = i;
     120             :     }
     121             :     std::wstring what(){
     122             :         return tek;
     123             :     }
     124           4 :     std::wstring next(std::wstring plop){
     125             :         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
     126             :         size_t n;
     127           4 :         std::wstring palautus;
     128           4 :         if (p < tek.size())
     129             :         {  
     130             :             //std::cout<<"\tp<tek.size()"<<std::endl;
     131           4 :             if ((n = tek.find(plop, p)) == std::wstring::npos || plop == L"")
     132             :             {  
     133             :                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
     134           3 :                 n = tek.size();
     135             :             }
     136             :             else
     137             :             {  
     138             :                 //std::cout<<"\t\tn != string::npos"<<std::endl;
     139             :             }
     140           4 :             palautus = tek.substr(p, n-p);
     141           4 :             p = n + plop.length();
     142             :         }
     143             :         //else
     144             :             //std::cout<<"\tp>=tek.size()"<<std::endl;
     145             :                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
     146           4 :         return palautus;
     147             :     }
     148             :     
     149             :     std::wstring next_esc(std::wstring plop) {
     150             :                 size_t n, realp;
     151             :                 
     152             :         if (p >= tek.size())
     153             :                 return L"";
     154             :                 
     155             :                 realp = p;
     156             :                 do {
     157             :                         n = tek.find(plop, p);
     158             :                         if (n == std::wstring::npos || plop == L"")
     159             :                                 n = tek.length();
     160             :                         p = n + plop.length();
     161             :                 } while (n > 0 && tek[n - 1] == '\\');
     162             :                 
     163             :                 return tek.substr(realp, n - realp);
     164             :     }
     165             :     
     166           8 :     bool atend(){
     167           8 :         if(p>=tek.size()) return true;
     168           4 :         return false;
     169             :     }
     170           4 :     WStrfnd(std::wstring s){
     171           4 :         start(s);
     172           4 :     }
     173             : };
     174             : 
     175             : #endif
     176             : 

Generated by: LCOV version 1.11