From: Jan Nieuwenhuizen Date: Wed, 25 Jan 2006 10:08:46 +0000 (+0000) Subject: * flower/include/std-string.hh: String/std::string compatibility: X-Git-Tag: release/2.7.29~20 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=bf898a6f737ebdd6a749ea0a14d1056a3566a10e;p=lilypond.git * flower/include/std-string.hh: String/std::string compatibility: Use NPOS for `not found' (iso -1), use ssize for length () and pos type. * flower/rational.cc: * flower/include/rational.hh: * flower/offset.cc: * flower/include/offset.hh: * flower/interval.cc: * flower/include/interval.hh: * flower/string-convert.cc: * flower/include/string-convert.hh: Use std::string [interface]. Update callers. --- diff --git a/ChangeLog b/ChangeLog index bed2975262..6d86f5d9d5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2006-01-25 Jan Nieuwenhuizen + + * flower/include/std-string.hh: String/std::string compatibility: + Use NPOS for `not found' (iso -1), use ssize for length () and pos + type. + + * flower/rational.cc: + * flower/include/rational.hh: + + * flower/offset.cc: + * flower/include/offset.hh: + + * flower/interval.cc: + * flower/include/interval.hh: + + * flower/string-convert.cc: + * flower/include/string-convert.hh: Use std::string [interface]. + Update callers. + 2006-01-25 Heikki Junes * Documentation/index.html.in: clean up. diff --git a/flower/file-name.cc b/flower/file-name.cc index d10e5b4c6a..fbd31de4c8 100644 --- a/flower/file-name.cc +++ b/flower/file-name.cc @@ -94,24 +94,24 @@ File_name::File_name (Std_string file_name) #endif int i = file_name.find (ROOTSEP); - if (i >= 0) + if (i != NPOS) { - root_ = Std_string (file_name, 0, i); - file_name = Std_string (file_name, i + 1); + root_ = file_name.substr (0, i); + file_name = file_name.substr (i + 1); } i = file_name.rfind (DIRSEP); - if (i >= 0) + if (i != NPOS) { - dir_ = Std_string (file_name, 0, i); - file_name = Std_string (file_name, i + 1); + dir_ = file_name.substr (0, i); + file_name = file_name.substr (i + 1); } i = file_name.rfind ('.'); - if (i >= 0) + if (i != NPOS) { - base_ = Std_string (file_name, 0, i); - ext_ = Std_string (file_name, i + 1); + base_ = file_name.substr (0, i); + ext_ = file_name.substr (i + 1); } else base_ = file_name; diff --git a/flower/file-path.cc b/flower/file-path.cc index e1c387a5ae..19d9d9bfab 100644 --- a/flower/file-path.cc +++ b/flower/file-path.cc @@ -42,10 +42,10 @@ File_path::parse_path (Std_string p) while ((len = p.length ())) { int i = p.find (PATHSEP); - if (i < 0) + if (i == NPOS) i = len; - append (String (p, 0, i)); - p = String (p, i + 1); + append (p.substr (0, i)); + p = p.substr (min (int(len), i + 1)); } } @@ -113,7 +113,7 @@ File_path::find (Std_string name) const return name; #ifdef __MINGW32__ - if (name.find ('\\') >= 0) + if (name.find ('\\') != NPOS) programming_error ("file name not normalized: " + name); #endif /* __MINGW32__ */ diff --git a/flower/getopt-long.cc b/flower/getopt-long.cc index 8829c27ee9..b7abd93817 100644 --- a/flower/getopt-long.cc +++ b/flower/getopt-long.cc @@ -10,9 +10,8 @@ #include #include -using namespace std; - #include "config.hh" +#include "string.hh" #if !HAVE_GETTEXT inline char * diff --git a/flower/include/file-name.hh b/flower/include/file-name.hh index e6247986f9..b91eb35156 100644 --- a/flower/include/file-name.hh +++ b/flower/include/file-name.hh @@ -12,10 +12,6 @@ #include "array.hh" #include "std-string.hh" -#if 0// STD_STRING -#include "string.hh" -#endif - class File_name { public: diff --git a/flower/include/getopt-long.hh b/flower/include/getopt-long.hh index 3e5d7000f8..03b660b30d 100644 --- a/flower/include/getopt-long.hh +++ b/flower/include/getopt-long.hh @@ -4,9 +4,6 @@ #include #include "std-string.hh" -using namespace std; - -#include "string.hh" /** a struct this for initialising the commandline options. diff --git a/flower/include/interval.hh b/flower/include/interval.hh index 9190f28544..6351d35535 100644 --- a/flower/include/interval.hh +++ b/flower/include/interval.hh @@ -9,6 +9,8 @@ #include +#include "std-string.hh" + #include "flower-proto.hh" #include "drul-array.hh" @@ -22,7 +24,7 @@ struct Interval_t : public Drul_array Drul_array::elem_ref; static T infinity (); - static String T_to_string (T arg); + static Std_string T_to_string (T arg); T center () const; void translate (T t) { @@ -109,7 +111,7 @@ struct Interval_t : public Drul_array Drul_array da (elem (LEFT), elem (RIGHT)); return ::linear_combination (da, x); } - String to_string () const; + Std_string to_string () const; bool contains (T r) const; void negate () diff --git a/flower/include/interval.tcc b/flower/include/interval.tcc index ec760db5fc..9af4047a8c 100644 --- a/flower/include/interval.tcc +++ b/flower/include/interval.tcc @@ -11,13 +11,13 @@ #include +#include "interval.hh" +#include "string.hh" + // MacOS 10.3 problems: // #include using namespace std; -#include "interval.hh" -#include "string.hh" - template int _Interval__compare (const Interval_t &a, Interval_t const &b) @@ -105,15 +105,15 @@ Interval_t::intersect (Interval_t h) } template -String +Std_string Interval_t::to_string () const { if (is_empty ()) return "[empty]"; - String s ("["); + Std_string s ("["); - return (s + T_to_string (elem (LEFT)) + String (",") - + T_to_string (elem (RIGHT)) + String ("]")); + return (s + T_to_string (elem (LEFT)) + Std_string (",") + + T_to_string (elem (RIGHT)) + Std_string ("]")); } template diff --git a/flower/include/offset.hh b/flower/include/offset.hh index 65a856ecac..5ca7e0e68c 100644 --- a/flower/include/offset.hh +++ b/flower/include/offset.hh @@ -8,9 +8,11 @@ #define OFFSET_HH #include "axis.hh" +#include "std-string.hh" #include "string.hh" #include "real.hh" +class Offset; Offset complex_multiply (Offset, Offset); Offset complex_divide (Offset, Offset); Offset complex_exp (Offset); @@ -92,7 +94,7 @@ public: coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS] = 0.0; } - String to_string () const; + Std_string to_string () const; Offset &mirror (Axis a) { diff --git a/flower/include/rational.hh b/flower/include/rational.hh index d29c7467e9..6c0da7b9a9 100644 --- a/flower/include/rational.hh +++ b/flower/include/rational.hh @@ -9,6 +9,8 @@ #ifndef RATIONAL_HH #define RATIONAL_HH +#include "std-string.hh" + #include "string.hh" /** @@ -67,7 +69,7 @@ public: Rational &operator %= (Rational); static int compare (Rational const &, Rational const &); int sign () const; - String to_string () const; + Std_string to_string () const; }; IMPLEMENT_ARITHMETIC_OPERATOR (Rational, /); diff --git a/flower/include/std-string.hh b/flower/include/std-string.hh index d623145479..4cefb79953 100644 --- a/flower/include/std-string.hh +++ b/flower/include/std-string.hh @@ -14,12 +14,19 @@ #define Std_string String #define to_std_string to_string class String; +typedef int ssize; +#define NPOS -1 + #include "string.hh" #else #include // #warning Using std::string +using namespace std; +typedef size_t ssize; + +#define NPOS std::string::npos namespace std { diff --git a/flower/include/string-convert.hh b/flower/include/string-convert.hh index 5bc8a7dd56..1e9da716ef 100644 --- a/flower/include/string-convert.hh +++ b/flower/include/string-convert.hh @@ -9,42 +9,43 @@ #include using namespace std; +#include "std-string.hh" #include "string.hh" -/** The functor String_convert handles all conversions to/from String +/** The functor Std_string_convert handles all conversions to/from Std_string (some time, anyway). The class is quite empty from data view. */ class String_convert { - static int hex2bin (String hex_string, String &bin_string_r); + static int hex2bin (Std_string hex_string, Std_string &bin_string_r); static int hex2nibble (Byte byte); static Byte nibble2hex_byte (Byte byte); public: - static String pad_to (String s, int length); - static String bool_string (bool b); - static String bin2dec (String bin_string); - static String bin2hex (String bin_string); - static String dec2bin (String str); - static int bin2int (String bin_string); - static unsigned bin2unsigned (String bin_string); - static String char_string (char c, int n); - static int dec2int (String dec_string); - static double dec2double (String dec_string); - static String double_string (double f, char const *fmt = 0); - static String form_string (char const *format, ...); - static String vform_string (char const *format, va_list args); - static int hex2int (String str); - static unsigned hex2unsigned (String str); - static String hex2bin (String str); - static String int_string (int i, char const *fmt = 0); - static String long_string (long); - static String int2hex (int i, int length_i, char ch); - static String unsigned2hex (unsigned u, int length_i, char ch); - static String int2dec (int i, int length_i, char ch); - static String rational_string (Rational); - static String pointer_string (void const *); - static String precision_string (double x, int n); - static Array split (String str, char c); - static String i64_string (I64, char const *fmt = 0); + static Std_string pad_to (Std_string s, int length); + static Std_string bool_string (bool b); + static Std_string bin2dec (Std_string bin_string); + static Std_string bin2hex (Std_string bin_string); + static Std_string dec2bin (Std_string str); + static int bin2int (Std_string bin_string); + static unsigned bin2unsigned (Std_string bin_string); + static Std_string char_string (char c, int n); + static int dec2int (Std_string dec_string); + static double dec2double (Std_string dec_string); + static Std_string double_string (double f, char const *fmt = 0); + static Std_string form_string (char const *format, ...); + static Std_string vform_string (char const *format, va_list args); + static int hex2int (Std_string str); + static unsigned hex2unsigned (Std_string str); + static Std_string hex2bin (Std_string str); + static Std_string int_string (int i, char const *fmt = 0); + static Std_string long_string (long); + static Std_string int2hex (int i, int length_i, char ch); + static Std_string unsigned2hex (unsigned u, ssize length, char ch); + static Std_string int2dec (int i, int length_i, char ch); + static Std_string rational_string (Rational); + static Std_string pointer_string (void const *); + static Std_string precision_string (double x, int n); + static Array split (Std_string str, char c); + static Std_string i64_string (I64, char const *fmt = 0); }; #endif // __STRING_CONVERT_HH // diff --git a/flower/include/string.hh b/flower/include/string.hh index 4a4f36f86e..b541368d7e 100644 --- a/flower/include/string.hh +++ b/flower/include/string.hh @@ -19,8 +19,6 @@ class ostream; #endif #include "std-string.hh" -using namespace std; - #include "arithmetic-operator.hh" #include "string-handle.hh" @@ -67,13 +65,16 @@ public: /* std::string interface */ char const *c_str () const; + char const *data () const; bool empty () const; int find (String &s, int pos=0) const; int find (char c) const; int rfind (char c) const; String replace (int pos, int n, String str); - String (String const &, int pos, int n=-1); + String substr (int pos=0, int n=NPOS) const; + + //String (String const &, int pos, int n=NPOS); String (int n, char c); protected: @@ -132,9 +133,7 @@ public: /// signed comparison, analogous to memcmp; static int compare (String const &s1, const String &s2); - /// index of rightmost element of string (???) - int index_last (char const *string) const; - + /// index of rightmost character C in string int index_last (char c) const; int index (char c) const; diff --git a/flower/interval.cc b/flower/interval.cc index aaa552353d..a3dc129019 100644 --- a/flower/interval.cc +++ b/flower/interval.cc @@ -18,7 +18,7 @@ Interval_t::infinity () } template<> -String +Std_string Interval_t::T_to_string (Real r) { return ::to_string (r); @@ -32,7 +32,7 @@ Interval_t::infinity () } template<> -String +Std_string Interval_t::T_to_string (int i) { return ::to_string (i); @@ -40,4 +40,3 @@ Interval_t::T_to_string (int i) template INTERVAL__INSTANTIATE (int); template INTERVAL__INSTANTIATE (Real); - diff --git a/flower/offset.cc b/flower/offset.cc index 62862327ac..9265f12fda 100644 --- a/flower/offset.cc +++ b/flower/offset.cc @@ -10,11 +10,11 @@ #ifndef STANDALONE -String +Std_string Offset::to_string () const { - String s; - s = String (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", " + Std_string s; + s = Std_string (" (") + ::to_string (coordinate_a_[X_AXIS]) + ", " + ::to_string (coordinate_a_[Y_AXIS]) + ")"; return s; } diff --git a/flower/rational.cc b/flower/rational.cc index b61c84e014..8ecdbccf34 100644 --- a/flower/rational.cc +++ b/flower/rational.cc @@ -269,16 +269,16 @@ Rational::operator -= (Rational r) return (*this += r); } -String +Std_string Rational::to_string () const { if (is_infinity ()) { - String s (sign_ > 0 ? "" : "-"); - return String (s + "infinity"); + Std_string s (sign_ > 0 ? "" : "-"); + return Std_string (s + "infinity"); } - String s = ::to_string (num ()); + Std_string s = ::to_string (num ()); if (den () != 1 && num ()) s += "/" + ::to_string (den ()); return s; diff --git a/flower/std-string.cc b/flower/std-string.cc index 62edc829be..66b94fda31 100644 --- a/flower/std-string.cc +++ b/flower/std-string.cc @@ -20,21 +20,15 @@ namespace std { return Std_string (n, c); } -#define FIND_FAILED string::npos -#define SIZE_T size_t #else /* !STD_STRING */ -#define FIND_FAILED -1 -#define SIZE_T int - #endif /* STD_STRING */ Std_string & replace_all (Std_string &str, Std_string find, Std_string replace) { - int len = find.length (); - for (SIZE_T i = str.find (find); i != FIND_FAILED; i = str.find (find, - i + len)) + ssize len = find.length (); + for (ssize i = str.find (find); i != NPOS; i = str.find (find, i + len)) str = str.replace (i, len, replace); return str; } diff --git a/flower/string-convert.cc b/flower/string-convert.cc index 91ef7e7f93..5f74cc9fa6 100644 --- a/flower/string-convert.cc +++ b/flower/string-convert.cc @@ -27,18 +27,18 @@ using namespace std; */ static const int STRING_BUFFER_LEN = 1024; -String +Std_string String_convert::bool_string (bool b) { - return String (b ? "true" : "false"); + return Std_string (b ? "true" : "false"); } -String -String_convert::bin2hex (String bin_string) +Std_string +String_convert::bin2hex (Std_string bin_string) { - String str; - Byte const *byte = bin_string.to_bytes (); - for (int i = 0; i < bin_string.length (); i++) + Std_string str; + Byte const *byte = (Byte const*)bin_string.data (); + for (ssize i = 0; i < bin_string.length (); i++) { str += to_string ((char)nibble2hex_byte (*byte >> 4)); str += to_string ((char)nibble2hex_byte (*byte++)); @@ -47,18 +47,18 @@ String_convert::bin2hex (String bin_string) } int -String_convert::bin2int (String bin_string) +String_convert::bin2int (Std_string bin_string) { return bin2unsigned (bin_string); } unsigned -String_convert::bin2unsigned (String bin_string) +String_convert::bin2unsigned (Std_string bin_string) { assert (bin_string.length () <= (int)sizeof (unsigned)); unsigned result_u = 0; - for (int i = 0; i < bin_string.length (); i++) + for (ssize i = 0; i < bin_string.length (); i++) { result_u <<= 8; result_u += (Byte)bin_string[ i ]; @@ -66,9 +66,9 @@ String_convert::bin2unsigned (String bin_string) return result_u; } -// breendet imp from String +// breendet imp from Std_string int -String_convert::dec2int (String dec_string) +String_convert::dec2int (Std_string dec_string) { if (!dec_string.length ()) return 0; @@ -80,17 +80,17 @@ String_convert::dec2int (String dec_string) return (int)l; } -String +Std_string String_convert::i64_string (I64 i64, char const *fmt) { char buffer[STRING_BUFFER_LEN]; snprintf (buffer, STRING_BUFFER_LEN, (fmt ? fmt : "%Ld"), i64); // assume radix 10 - return String (buffer); + return Std_string (buffer); } -// breendet imp from String +// breendet imp from Std_string double -String_convert::dec2double (String dec_string) +String_convert::dec2double (Std_string dec_string) { if (!dec_string.length ()) return 0; @@ -101,14 +101,14 @@ String_convert::dec2double (String dec_string) } int -String_convert::hex2bin (String hex_string, String &bin_string_r) +String_convert::hex2bin (Std_string hex_string, Std_string &bin_string_r) { if (hex_string.length () % 2) hex_string = "0" + hex_string; bin_string_r = ""; - Byte const *byte = hex_string.to_bytes (); - int i = 0; + Byte const *byte = (Byte const*) hex_string.data (); + ssize i = 0; while (i < hex_string.length ()) { int high_i = hex2nibble (*byte++); @@ -121,10 +121,10 @@ String_convert::hex2bin (String hex_string, String &bin_string_r) return 0; } -String -String_convert::hex2bin (String hex_string) +Std_string +String_convert::hex2bin (Std_string hex_string) { - String str; + Std_string str; // silly, asserts should alway be "on"! // assert (!hex2bin (hex_string, str) ); int error_i = hex2bin (hex_string, str); @@ -145,7 +145,7 @@ String_convert::hex2nibble (Byte byte) } // stupido. Should use int_string () -String +Std_string String_convert::int2dec (int i, int length_i, char ch) { char fill_char = ch; @@ -153,17 +153,17 @@ String_convert::int2dec (int i, int length_i, char ch) fill_char = '0'; // ugh - String dec_string = to_string (i); + Std_string dec_string = to_string (i); // ugh return to_string (fill_char, length_i - dec_string.length ()) + dec_string; } // stupido. Should use int_string () -String -String_convert::unsigned2hex (unsigned u, int length_i, char fill_char) +Std_string +String_convert::unsigned2hex (unsigned u, ssize length, char fill_char) { - String str; + Std_string str; if (!u) str = "0"; @@ -177,14 +177,14 @@ String_convert::unsigned2hex (unsigned u, int length_i, char fill_char) str += int_string (u, "%x"); // hmm. %lx vs. %x -> portability? #endif - str = to_string (fill_char, length_i - str.length ()) + str; - while ((str.length () > length_i) && (str[ 0 ] == 'f')) - str = str.cut_string (2, INT_MAX); + str = to_string (fill_char, length - str.length ()) + str; + while ((str.length () > length) && (str[ 0 ] == 'f')) + str = str.substr (2); return str; } -String +Std_string String_convert::int2hex (int i, int length_i, char fill_char) { return unsigned2hex ((unsigned)i, length_i, fill_char); @@ -204,16 +204,16 @@ String_convert::nibble2hex_byte (Byte byte) @param #fmt# is a printf style format, default assumes "%d" as format. */ -String +Std_string String_convert::int_string (int i, char const *fmt) { char buffer[STRING_BUFFER_LEN]; snprintf (buffer, STRING_BUFFER_LEN, (fmt ? fmt : "%d"), i); // assume radix 10 - return String (buffer); + return Std_string (buffer); } -String +Std_string String_convert::form_string (char const *format, ...) { va_list args; @@ -221,15 +221,15 @@ String_convert::form_string (char const *format, ...) char buffer[STRING_BUFFER_LEN]; vsnprintf (buffer, STRING_BUFFER_LEN, format, args); va_end (args); - return String (buffer); + return Std_string (buffer); } -String +Std_string String_convert::vform_string (char const *format, va_list args) { char buffer[STRING_BUFFER_LEN]; vsnprintf (buffer, STRING_BUFFER_LEN, format, args); - return String (buffer); + return Std_string (buffer); } /** @@ -237,13 +237,13 @@ String_convert::vform_string (char const *format, va_list args) @param #fmt# is a printf style format, default assumes "%lf" as format */ -String +Std_string String_convert::double_string (double f, char const *fmt) { char buf[STRING_BUFFER_LEN]; snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f); - return String (buf); + return Std_string (buf); } /** @@ -252,29 +252,33 @@ String_convert::double_string (double f, char const *fmt) @param #n# is a repetition count, default value is 1 */ -String +Std_string String_convert::char_string (char c, int n) { n = n >= 0 ? n : 0; char *ch = new char[ n ]; memset (ch, c, n); - String s ((Byte *)ch, n); +#if STD_STRING + Std_string s (ch, n); +#else + Std_string s ((Byte*) ch, n); +#endif delete[] ch; return s; } -String +Std_string String_convert::rational_string (Rational r) { return r.to_string (); } -String +Std_string String_convert::pointer_string (void const *l) { char buffer[STRING_BUFFER_LEN]; snprintf (buffer, STRING_BUFFER_LEN, "%p", l); // assume radix 10 - return String (buffer); + return Std_string (buffer); } /** @@ -283,57 +287,57 @@ String_convert::pointer_string (void const *l) @param #n# is the number of nonzero digits */ -String +Std_string String_convert::precision_string (double x, int n) { - String format = "%." + to_string (max (0, n - 1)) + "e"; - String str = double_string (abs (x), format.c_str ()); + Std_string format = "%." + to_string (max (0, n - 1)) + "e"; + Std_string str = double_string (abs (x), format.c_str ()); - int exp = str.right_string (3).to_int (); - str = str.left_string (str.length () - 4); + int exp = dec2int (str.substr (str.length () - 3)); + str = str.substr (0, str.length () - 4); while (str[str.length () - 1] == '0') - str = str.left_string (str.length () - 1); + str = str.substr (0, str.length () - 1); if (str[str.length () - 1] == '.') - str = str.left_string (str.length () - 1); + str = str.substr (0, str.length () - 1); if (exp == 0) return (sign (x) > 0 ? str : "-" + str); - str = str.left_string (1) + str.cut_string (2, INT_MAX); - int dot = 1 + exp; + str = str.substr (0, 1) + str.substr (2); + ssize dot = 1 + exp; if (dot <= 0) str = "0." + to_string ('0', -dot) + str; else if (dot >= str.length ()) str += to_string ('0', dot - str.length ()); else if ((dot > 0) && (dot < str.length ())) - str = str.left_string (dot) + "." + str.cut_string (dot, INT_MAX); + str = str.substr (0, dot) + "." + str.substr (dot); else assert (0); return (sign (x) > 0 ? str : "-" + str); } -Array -String_convert::split (String str, char c) +Array +String_convert::split (Std_string str, char c) { - Array a; - int i = str.index (c); - while (i >= 0) + Array a; + ssize i = str.find (c); + while (i != NPOS) { - String s = str.left_string (i); + Std_string s = str.substr (0, i); a.push (s); while (str[++i] == c) ; - str = str.cut_string (i, INT_MAX); - i = str.index (c); + str = str.substr (i); + i = str.find (c); } if (str.length ()) a.push (str); return a; } -String +Std_string String_convert::long_string (long l) { char s[STRING_BUFFER_LEN]; @@ -341,8 +345,8 @@ String_convert::long_string (long l) return s; } -String -String_convert::pad_to (String s, int n) +Std_string +String_convert::pad_to (Std_string s, int n) { - return s + to_string (' ', max (n - s.length (), 0)); + return s + Std_string (max (int(n - s.length ()), 0), ' '); } diff --git a/flower/string.cc b/flower/string.cc index e3e8057f7e..ec4bd90243 100644 --- a/flower/string.cc +++ b/flower/string.cc @@ -41,15 +41,35 @@ String::operator Std_string () const /* std::string interface */ +#if 0 +// FIXME, use .SUBSTR () ? String::String (String const &s, int pos, int n) { - if (n == -1) + if (n == -1 || n == INT_MAX || n == NPOS) n = s.length () - pos; if (pos == 0) *this = s.left_string (n); else *this = s.right_string (s.length () - pos).left_string (n); } +#endif + +String +String::substr (int pos, int n) const +{ +#if 0 + if (n == -1 || n == INT_MAX || n == NPOS) + n = length () - pos; + return cut_string (pos, n); +#else + if (n == -1 || n == INT_MAX || n == NPOS) + n = length () - pos; + if (pos == 0) + return left_string (n); + else + return right_string (length () - pos).left_string (n); +#endif +} String::String (int n, char c) { @@ -57,6 +77,12 @@ String::String (int n, char c) } +char const * +String::data () const +{ + return (char const*) to_bytes (); +} + bool String::empty () const { @@ -76,9 +102,9 @@ String::find (String &s, int pos) const return index (s); String f = right_string (length () - pos); int n = f.index (s); - if (n != -1) + if (n != NPOS) return pos + n; - return -1; + return NPOS; } int @@ -90,7 +116,7 @@ String::rfind (char c) const String String::replace (int pos, int n, String str) { - return String (*this, 0, pos) + str + String (*this, pos + n); + return this->substr (0, pos) + str + this->substr (pos + n); } @@ -245,41 +271,20 @@ int String::index_last (char const c) const { if (!length ()) - return -1; + return NPOS; char const *me = strh_.c_str (); char const *p = (char const *)memrchr ((Byte *)me, length (), c); if (p) return p - me; - return -1; -} - -int -String::index_last (char const *string) const // UGK! -{ - assert (false); // broken - int len = strlen (string); // ugrh - if (!length () || !len) - return -1; - - int next_i = index (string); - if (next_i == -1) - return -1; - - int index_i = 0; - while (next_i >= 0) - { - index_i += next_i; - next_i = right_string (length () - index_i - len).index (string); - } - return index_i; + return NPOS; } /** find a character. @return the index of the leftmost character #c# (0 <= return < length ()), -or -1 if not found. +or NPOS if not found. ? should return length ()?, as in string.left_string (index (delimiter)) */ @@ -290,7 +295,7 @@ String::index (char c) const char const *p = (char const *) memchr (me, c, length ()); if (p) return p - me; - return -1; + return NPOS; } /** @@ -311,14 +316,14 @@ String::index (String searchfor) const if (p) return p - me; - return -1; + return NPOS; } /** find chars of a set. @return -the index of the leftmost occurance of an element of #set#. -1 if +the index of the leftmost occurance of an element of #set#. NPOS if nothing is found. */ int @@ -326,7 +331,7 @@ String::index_any (String set) const { int n = length (); if (!n) - return -1; + return NPOS; void const *me = (void const *) strh_.c_str (); for (int i = 0; i < set.length (); i++) @@ -335,7 +340,7 @@ String::index_any (String set) const if (found) return found - (char const *)me; } - return -1; + return NPOS; } String @@ -452,7 +457,7 @@ String::substitute (String find, String replace) { int n = find.length (); int m = replace.length (); - for (int i = index (find), j = 0; i > -1; + for (int i = index (find), j = 0; i != NPOS; i = right_string (length () - j).index (find)) { *this = left_string (i + j) @@ -466,7 +471,7 @@ String::substitute (String find, String replace) String String::substitute (char find, char replace) { - for (int i = index (find); i > - 1; i = index (find)) + for (int i = index (find); i != NPOS; i = index (find)) (*this)[i] = replace; return *this; } diff --git a/lily/include/lily-guile.hh b/lily/include/lily-guile.hh index 2b1afe115e..de3d8f0e8c 100644 --- a/lily/include/lily-guile.hh +++ b/lily/include/lily-guile.hh @@ -23,7 +23,6 @@ #define GUILE_ELLIPSIS #endif - #include "guile-compatibility.hh" #include "interval.hh" #include "lily-guile-macros.hh" diff --git a/lily/lily-guile.cc b/lily/lily-guile.cc index f1c0488e3f..faa02a66e6 100644 --- a/lily/lily-guile.cc +++ b/lily/lily-guile.cc @@ -356,7 +356,7 @@ ly_assoc_cdr (SCM key, SCM alist) } SCM -ly_string_array_to_scm (Array a) +ly_string_array_to_scm (Array a) { SCM s = SCM_EOL; for (int i = a.size () - 1; i >= 0; i--) diff --git a/lily/lilypond-version.cc b/lily/lilypond-version.cc index 93a5ceba88..da44cba73b 100644 --- a/lily/lilypond-version.cc +++ b/lily/lilypond-version.cc @@ -25,18 +25,18 @@ Lilypond_version::Lilypond_version (String str) minor_ = 0; patch_ = 0; - Array version; + Array version; version = String_convert::split (str, '.'); if (version.size () > 0 && isdigit (version[0][0])) - major_ = version[0].to_int (); + major_ = String_convert::dec2int (version[0]); if (version.size () > 1 && isdigit (version[1][0])) - minor_ = version[1].to_int (); + minor_ = String_convert::dec2int (version[1]); patch_ = 0; if (version.size () >= 3 && isdigit (version[2][0])) - patch_ = version[2].to_int (); + patch_ = String_convert::dec2int (version[2]); if (version.size () >= 4) extra_patch_string_ = version[3]; diff --git a/lily/main.cc b/lily/main.cc index daac5b01c0..fa0d356f0a 100644 --- a/lily/main.cc +++ b/lily/main.cc @@ -299,7 +299,7 @@ do_chroot_jail () USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX }; - Array components = String_convert::split (jail_spec, ','); + Array components = String_convert::split (jail_spec, ','); if (components.size () != JAIL_MAX) { error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX, diff --git a/lily/template5.cc b/lily/template5.cc index 1c1d15d1f6..d2581833b6 100644 --- a/lily/template5.cc +++ b/lily/template5.cc @@ -20,7 +20,7 @@ Interval_t::infinity () } template<> -String +Std_string Interval_t::T_to_string (Rational a) { return a.to_string ();