[project @ 2005-02-28 05:48:26 by unknown_lamer]
[clinton/bobotpp.git] / source / String.C
index d208288..060d820 100644 (file)
@@ -1,5 +1,6 @@
 // String.C  -*- C++ -*-
 // Copyright (c) 1997, 1998 Etienne BERNARD
+// Copyright (C) 2002,2005 Clinton Ebadi
 
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -39,9 +40,13 @@ String::String(const char *s)
 String::String(const std::string & s)
 {
   p = new srep;
-  len = s.length ();
+  // We do this instead of just s.length () because there might be a
+  // \0 in the string before the end (e.g. this is a message from the
+  // Socket's buffer).
+  const char* temp_str = s.c_str ();
+  len = strlen (temp_str);
   p->s = new char[len + 1];
-  std::strcpy (p->s, s.c_str ());
+  std::strcpy (p->s, temp_str);
 }
 
 String::String(const String & s)
@@ -182,12 +187,24 @@ String::subString(int debut, int fin)
   return res;
 }
 
+String
+String::substr (int s, int e)
+{
+  return subString (s, e);
+}
+
 String
 String::subString(int debut)
 {
   return subString(debut, len - 1);
 }
 
+String
+String::substr (int s)
+{
+  return subString (s);
+}
+
 String
 String::toLower()
 {
@@ -228,7 +245,7 @@ String::trim()
   while (i < j && (p->s[i] == ' ' || p->s[i] == '\t' || p->s[i] == '\r'))
     i++;
   
-  while (j > 0 && (p->s[j] == ' ' || p->s[j] == '\t' || p->s[i] == '\r'))
+  while (j > 0 && (p->s[j] == ' ' || p->s[j] == '\t' || p->s[j] == '\r'))
     j--;
   
   return subString(i, j);