write rfind in terms of npos as daniel requested
[oota-llvm.git] / include / llvm / ADT / StringRef.h
index 2015a66bd82c02493be9da1c6bfa6f6a40b839a6..e4fd258cb87182ef27a6c01e0ec5554f2eb4ff41 100644 (file)
@@ -28,7 +28,8 @@ namespace llvm {
   public:
     typedef const char *iterator;
     static const size_t npos = ~size_t(0);
-
+    typedef size_t size_type;
+    
   private:
     /// The start of the string, in an external buffer.
     const char *Data;
@@ -162,45 +163,45 @@ namespace llvm {
     ///
     /// \return - The index of the first occurence of \arg Str, or npos if not
     /// found.
-    size_t find(const StringRef &Str) const {
-      size_t N = Str.size();
-      if (N > Length)
-        return npos;
-      for (size_t i = 0, e = Length - N + 1; i != e; ++i)
-        if (substr(i, N).equals(Str))
-          return i;
-      return npos;
-    }
-
+    size_t find(const StringRef &Str) const;
+    
     /// rfind - Search for the last character \arg C in the string.
     ///
     /// \return - The index of the last occurence of \arg C, or npos if not
     /// found.
-    size_t rfind(char C) const {
-      for (size_t i = Length, e = 0; i != e;) {
+    size_t rfind(char C, size_t From = npos) const {
+      From = std::min(From, Length);
+      size_t i = From;
+      while (i != 0) {
         --i;
         if (Data[i] == C)
           return i;
       }
       return npos;
     }
-
+    
     /// rfind - Search for the last string \arg Str in the string.
     ///
     /// \return - The index of the last occurence of \arg Str, or npos if not
     /// found.
-    size_t rfind(const StringRef &Str) const {
-      size_t N = Str.size();
-      if (N > Length)
-        return npos;
-      for (size_t i = Length - N + 1, e = 0; i != e;) {
-        --i;
-        if (substr(i, N).equals(Str))
-          return i;
-      }
-      return npos;
-    }
-
+    size_t rfind(const StringRef &Str) const;
+    
+    /// find_first_of - Find the first instance of the specified character or
+    /// return npos if not in string.  Same as find.
+    size_type find_first_of(char C) const { return find(C); }
+    
+    /// find_first_of - Find the first character from the string 'Chars' in the
+    /// current string or return npos if not in string.
+    size_type find_first_of(StringRef Chars) const;
+    
+    /// find_first_not_of - Find the first character in the string that is not
+    /// in the string 'Chars' or return npos if all are in string. Same as find.
+    size_type find_first_not_of(StringRef Chars) const;
+    
+    /// @}
+    /// @name Helpful Algorithms
+    /// @{
+    
     /// count - Return the number of occurrences of \arg C in the string.
     size_t count(char C) const {
       size_t Count = 0;
@@ -209,23 +210,10 @@ namespace llvm {
           ++Count;
       return Count;
     }
-
+    
     /// count - Return the number of non-overlapped occurrences of \arg Str in
     /// the string.
-    size_t count(const StringRef &Str) const {
-      size_t Count = 0;
-      size_t N = Str.size();
-      if (N > Length)
-        return 0;
-      for (size_t i = 0, e = Length - N + 1; i != e; ++i)
-        if (substr(i, N).equals(Str))
-          ++Count;
-      return Count;
-    }
-
-    /// @}
-    /// @name Helpful Algorithms
-    /// @{
+    size_t count(const StringRef &Str) const;
     
     /// getAsInteger - Parse the current string as an integer of the specified
     /// radix.  If Radix is specified as zero, this does radix autosensing using
@@ -235,8 +223,10 @@ namespace llvm {
     /// this returns true to signify the error.  The string is considered
     /// erroneous if empty.
     ///
-    //bool getAsInteger(unsigned Radix, long long &Result) const;
+    bool getAsInteger(unsigned Radix, long long &Result) const;
     bool getAsInteger(unsigned Radix, unsigned long long &Result) const;
+    bool getAsInteger(unsigned Radix, int &Result) const;
+    bool getAsInteger(unsigned Radix, unsigned &Result) const;
 
     // TODO: Provide overloads for int/unsigned that check for overflow.