Back to index

texmacs  1.0.7.15
parse_string.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : parse_string.cpp
00004 * DESCRIPTION: strings from which it is both easy to read and write characters
00005 *              they are used for entity replacement in the XML parser
00006 * COPYRIGHT  : (C) 2005  Joris van der Hoeven
00007 *******************************************************************************
00008 * This software falls under the GNU general public license version 3 or later.
00009 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00010 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00011 ******************************************************************************/
00012 
00013 #include "parse_string.hpp"
00014 #include "analyze.hpp"
00015 
00016 void
00017 parse_string_rep::advance (int n) {
00018   if (is_nil (l) || n <= 0) return;
00019   p->item += n;
00020   if (p->item >= N (l->item)) {
00021     n= p->item - N (l->item);
00022     l= l->next;
00023     p= p->next;
00024     advance (n);
00025   }
00026 }
00027 
00028 string
00029 parse_string_rep::read (int n) {
00030   string s;
00031   while (!is_nil (l) && p->item + n > N (l->item)) {
00032     s << l->item (p->item, N (l->item));
00033     n -= (N (l->item) - p->item);
00034     l  = l->next;
00035     p  = p->next;
00036   }
00037   if (is_nil (l)) return s;
00038   s << l->item (p->item, p->item + n);
00039   p->item += n;
00040   if (p->item >= N(l->item)) {
00041     l= l->next;
00042     p= p->next;
00043   }
00044   return s;
00045 }
00046 
00047 void
00048 parse_string_rep::write (string s) {
00049   if (N(s) > 0) {
00050     l= list<string> (s, l);
00051     p= list<int>    (0, p);
00052   }
00053 }
00054 
00055 char
00056 parse_string_rep::get_char (int n) {
00057   if (is_nil (l)) return 0;
00058   if (p->item + n < N (l->item))
00059     return l->item [p->item + n];
00060 
00061   list<string> ll= l;
00062   list<int>    pp= p;
00063   while (!is_nil (l) && pp->item + n >= N (ll->item)) {
00064     n -= (N (ll->item) - pp->item);
00065     ll = ll->next;
00066     pp = pp->next;
00067   }
00068   if (is_nil (ll)) return 0;
00069   return ll->item [pp->item + n];
00070 }
00071 
00072 string
00073 parse_string_rep::get_string (int n) {
00074   if (is_nil (l)) return "";
00075   if (p->item + n <= N (l->item))
00076     return l->item (p->item, p->item + n);
00077 
00078   string s;
00079   list<string> ll= l;
00080   list<int>    pp= p;
00081   while (n >= 0 && !is_nil (ll)) {
00082     int m= min (N (ll->item) - pp->item, n);
00083     s << ll->item (pp->item, pp->item + m);
00084     n -= m;
00085     ll = ll->next;
00086     pp = pp->next;
00087   }
00088   return s;
00089 }
00090 
00091 bool
00092 parse_string_rep::test (string s) {
00093   if (is_nil (l)) return N(s) == 0;
00094   if (p->item + N(s) <= N (l->item))
00095     return ::test (l->item, p->item, s);
00096 
00097   return get_string (N(s)) == s;
00098 }
00099 
00100 bool
00101 test (parse_string s, string what) {
00102   return s->test (what);
00103 }
00104 
00105 tm_ostream&
00106 operator << (tm_ostream& out, parse_string s) {
00107   list<string> l= s->l;
00108   list<int>    p= s->p;
00109   while (!is_nil (l)) {
00110     out << l->item (p->item, N(l->item));
00111     l= l->next;
00112     p= p->next;
00113   }
00114   return out;
00115 }