]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Add [] operator for offset-based access into String's.
authoramosjeffries <>
Sat, 19 Jan 2008 17:38:32 +0000 (17:38 +0000)
committeramosjeffries <>
Sat, 19 Jan 2008 17:38:32 +0000 (17:38 +0000)
Safer than pointer arithmetic and manipulation.
Also users of String objects should NOT have arbitrary access to the
underlying raw data buffer.

This single operator deprecates several old access functions.

src/SquidString.h
src/String.cci

index c2f371d2b2eaeade77b0772686782007ccb77e50..9d31866fcb8397d2082f0cda537b7e37b6106799 100644 (file)
@@ -1,6 +1,5 @@
-
 /*
- * $Id: SquidString.h,v 1.12 2007/11/04 23:59:51 amosjeffries Exp $
+ * $Id: SquidString.h,v 1.13 2008/01/19 10:38:32 amosjeffries Exp $
  *
  * DEBUG: section 67    String
  * AUTHOR: Duane Wessels
@@ -90,6 +89,12 @@ public:
     bool operator ==(String const &) const;
     bool operator !=(String const &) const;
 
+    /**
+     * Retrieve a single character in the string.
+     \param pos        Position of character to retrieve.
+     */
+    _SQUID_INLINE_ char &operator [](unsigned int pos);
+
     _SQUID_INLINE_ int size() const;
     _SQUID_INLINE_ char const * buf() const;
     void buf(char *);
@@ -112,10 +117,19 @@ public:
     _SQUID_INLINE_ int caseCmp (char const *) const;
     _SQUID_INLINE_ int caseCmp (char const *, size_t count) const;
 
+    /** \deprecated Use assignment to [] position instead.
+     *              ie   str[0] = 'h';
+     */
     _SQUID_INLINE_ void set(char const *loc, char const ch);
 
+    /** \deprecated Use assignment to [] position instead.
+     *              ie   str[newLength] = '\0';
+     */
     _SQUID_INLINE_ void cut(size_t newLength);
 
+    /** \deprecated Use assignment to [] position instead.
+     *              ie   str[newLength] = '\0';
+     */
     _SQUID_INLINE_ void cutPointer(char const *loc);
 
 #if DEBUGSTRINGS
index ccc48f4ac0b076ed7634b5bc2b9499b712393bd1..fd726d9ba9e755959d72f8f55cfda05b3f5bcf0e 100644 (file)
@@ -1,6 +1,5 @@
-
 /*
- * $Id: String.cci,v 1.10 2007/11/04 23:59:52 amosjeffries Exp $
+ * $Id: String.cci,v 1.11 2008/01/19 10:38:32 amosjeffries Exp $
  *
  * DEBUG: section 67    String
  * AUTHOR: Duane Wessels
@@ -52,6 +51,14 @@ String::buf() const
     return buf_;
 }
 
+char&
+String::operator [](unsigned int pos)
+{
+    assert(pos < size_);
+
+    return buf_[pos];
+}
+
 const char *
 String::pos(char const *aString) const
 {