From: amosjeffries <> Date: Sat, 19 Jan 2008 17:38:32 +0000 (+0000) Subject: Add [] operator for offset-based access into String's. X-Git-Tag: BASIC_TPROXY4~180 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=3db44fdf67906c85700b453fbcdf6cea0bcbd6e9;p=thirdparty%2Fsquid.git Add [] operator for offset-based access into String's. 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. --- diff --git a/src/SquidString.h b/src/SquidString.h index c2f371d2b2..9d31866fcb 100644 --- a/src/SquidString.h +++ b/src/SquidString.h @@ -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 diff --git a/src/String.cci b/src/String.cci index ccc48f4ac0..fd726d9ba9 100644 --- a/src/String.cci +++ b/src/String.cci @@ -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 {