+2006-01-25 Jan Nieuwenhuizen <janneke@gnu.org>
+
+ * 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 <hjunes@cc.hut.fi>
* Documentation/index.html.in: clean up.
#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;
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));
}
}
return name;
#ifdef __MINGW32__
- if (name.find ('\\') >= 0)
+ if (name.find ('\\') != NPOS)
programming_error ("file name not normalized: " + name);
#endif /* __MINGW32__ */
#include <cassert>
#include <cstdlib>
-using namespace std;
-
#include "config.hh"
+#include "string.hh"
#if !HAVE_GETTEXT
inline char *
#include "array.hh"
#include "std-string.hh"
-#if 0// STD_STRING
-#include "string.hh"
-#endif
-
class File_name
{
public:
#include <cstdio>
#include "std-string.hh"
-using namespace std;
-
-#include "string.hh"
/**
a struct this for initialising the commandline options.
#include <math.h>
+#include "std-string.hh"
+
#include "flower-proto.hh"
#include "drul-array.hh"
Drul_array<T>::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)
{
Drul_array<Real> 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 ()
#include <cassert>
+#include "interval.hh"
+#include "string.hh"
+
// MacOS 10.3 problems:
// #include <cmath>
using namespace std;
-#include "interval.hh"
-#include "string.hh"
-
template<class T>
int
_Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b)
}
template<class T>
-String
+Std_string
Interval_t<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<class T>
#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);
coordinate_a_[X_AXIS] = coordinate_a_[Y_AXIS] = 0.0;
}
- String to_string () const;
+ Std_string to_string () const;
Offset &mirror (Axis a)
{
#ifndef RATIONAL_HH
#define RATIONAL_HH
+#include "std-string.hh"
+
#include "string.hh"
/**
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, /);
#define Std_string String
#define to_std_string to_string
class String;
+typedef int ssize;
+#define NPOS -1
+
#include "string.hh"
#else
#include <string>
// #warning Using std::string
+using namespace std;
+typedef size_t ssize;
+
+#define NPOS std::string::npos
namespace std {
#include <cstdarg>
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<String> 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<Std_string> split (Std_string str, char c);
+ static Std_string i64_string (I64, char const *fmt = 0);
};
#endif // __STRING_CONVERT_HH //
#endif
#include "std-string.hh"
-using namespace std;
-
#include "arithmetic-operator.hh"
#include "string-handle.hh"
/* 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:
/// 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;
}
template<>
-String
+Std_string
Interval_t<Real>::T_to_string (Real r)
{
return ::to_string (r);
}
template<>
-String
+Std_string
Interval_t<int>::T_to_string (int i)
{
return ::to_string (i);
template INTERVAL__INSTANTIATE (int);
template INTERVAL__INSTANTIATE (Real);
-
#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;
}
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;
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;
}
*/
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++));
}
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 ];
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;
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;
}
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++);
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);
}
// stupido. Should use int_string ()
-String
+Std_string
String_convert::int2dec (int i, int length_i, char ch)
{
char fill_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";
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);
@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;
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);
}
/**
@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);
}
/**
@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);
}
/**
@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>
-String_convert::split (String str, char c)
+Array<Std_string>
+String_convert::split (Std_string str, char c)
{
- Array<String> a;
- int i = str.index (c);
- while (i >= 0)
+ Array<Std_string> 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];
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), ' ');
}
/* 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)
{
}
+char const *
+String::data () const
+{
+ return (char const*) to_bytes ();
+}
+
bool
String::empty () 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
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);
}
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))
*/
char const *p = (char const *) memchr (me, c, length ());
if (p)
return p - me;
- return -1;
+ return NPOS;
}
/**
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
{
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++)
if (found)
return found - (char const *)me;
}
- return -1;
+ return NPOS;
}
\f
String
{
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)
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;
}
#define GUILE_ELLIPSIS
#endif
-
#include "guile-compatibility.hh"
#include "interval.hh"
#include "lily-guile-macros.hh"
}
SCM
-ly_string_array_to_scm (Array<String> a)
+ly_string_array_to_scm (Array<Std_string> a)
{
SCM s = SCM_EOL;
for (int i = a.size () - 1; i >= 0; i--)
minor_ = 0;
patch_ = 0;
- Array<String> version;
+ Array<Std_string> 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];
USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
};
- Array<String> components = String_convert::split (jail_spec, ',');
+ Array<Std_string> components = String_convert::split (jail_spec, ',');
if (components.size () != JAIL_MAX)
{
error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX,
}
template<>
-String
+Std_string
Interval_t<Rational>::T_to_string (Rational a)
{
return a.to_string ();