/*
- * SBuf.h (C) 2008 Francesco Chemolli <kinkie@squid-cache.org>
- *
* SQUID Web Proxy Cache http://www.squid-cache.org/
* ----------------------------------------------------------
*
{
public:
typedef MemBlob::size_type size_type;
- static const size_type npos = -1;
+ static const size_type npos = 0xffffffff; // max(uint32_t)
/// Maximum size of a SBuf. By design it MUST be < MAX(size_type)/2. Currently 256Mb.
static const size_type maxSize = 0xfffffff;
* \param n how many bytes to import into the SBuf. If it is npos
* or unspecified, imports to end-of-cstring
* \note it is the caller's responsibility not to go out of bounds
- * \note bounds is 0 <= pos < length()
+ * \note bounds is 0 <= pos < length(); caller must pay attention to signedness
*/
explicit SBuf(const char *S, size_type n = npos);
/** random-access read to any char within the SBuf.
*
* \throw OutOfBoundsException when access is out of bounds
- * \note bounds is 0 <= pos < length()
+ * \note bounds is 0 <= pos < length(); caller must pay attention to signedness
*/
const char at(size_type pos) const {checkAccessBounds(pos); return operator[](pos);}
* \param pos the position to be overwritten
* \param toset the value to be written
* \throw OutOfBoundsException when pos is of bounds
- * \note bounds is 0 <= pos < length()
+ * \note bounds is 0 <= pos < length(); caller must pay attention to signedness
* \note performs a copy-on-write if needed.
*/
void setAt(size_type pos, char toset);
*/
char *rawSpace(size_type minSize);
- /**
+ /** Obtain how much free space is available in the backing store.
*
+ * \note: unless the client just cow()ed, it is not guaranteed that
+ * the free space can be used.
*/
size_type spaceSize() const { return store_->spaceSize(); }
* It is an in-place-modifying version of substr.
* \param pos start sub-stringing from this byte. If it is
* npos or it is greater than the SBuf length, the SBuf is cleared and
- * an empty SBuf is returned. If it is <0, it is ignored
+ * an empty SBuf is returned.
* \param n maximum number of bytes of the resulting SBuf.
* npos means "to end of SBuf".
* if it is 0, the SBuf is cleared and an empty SBuf is returned.
- * if it is < 0, it is ignored (same as supplying npos)
* if it overflows the end of the SBuf, it is capped to the end of SBuf
* \see substr, trim
*/
* sequence contained in the str argument.
* \param startPos if specified, ignore any occurrences before that position
* if startPos is npos or greater than length() npos is always returned
- * if startPos is < 0, it is ignored
* \return npos if the SBuf was not found
*/
size_type find(const SBuf & str, size_type startPos = 0) const;
* \return npos if the char was not found
* \param endPos if specified, ignore any occurrences after that position.
* if npos or greater than length(), the whole SBuf is considered
- * if < 0, npos is always returned
*/
size_type rfind(char c, size_type endPos = npos) const;
* \return npos if the sequence was not found
* \param endPos if specified, ignore any occurrences after that position
* if npos or greater than length(), the whole SBuf is considered
- * if < 0, npos is always returned
*/
size_type rfind(const SBuf &str, size_type endPos = npos) const;
* \return npos if no character in the set could be found
* \param startPos if specified, ignore any occurrences before that position
* if npos, then npos is always returned
- * if <0, it is ignored.
*/
size_type find_first_of(const SBuf &set, size_type startPos = 0) const;
// test accessors on empty SBuf.
{
SBuf s1;
- CPPUNIT_ASSERT_EQUAL(0,s1.length());
+ CPPUNIT_ASSERT_EQUAL(0U,s1.length());
CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
// TEST: copy-construct NULL string (implicit destructor non-crash test)
{
SBuf s1(NULL);
- CPPUNIT_ASSERT_EQUAL(0,s1.length());
+ CPPUNIT_ASSERT_EQUAL(0U,s1.length());
CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
// TEST: copy-construct empty string (implicit destructor non-crash test)
{
SBuf s1("");
- CPPUNIT_ASSERT_EQUAL(0,s1.length());
+ CPPUNIT_ASSERT_EQUAL(0U,s1.length());
CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
// TEST: copy-construct from a SBuf
{
SBuf s1(empty_sbuf);
- CPPUNIT_ASSERT_EQUAL(0,s1.length());
+ CPPUNIT_ASSERT_EQUAL(0U,s1.length());
CPPUNIT_ASSERT_EQUAL(SBuf(""),s1);
CPPUNIT_ASSERT_EQUAL(empty_sbuf,s1);
CPPUNIT_ASSERT_EQUAL(0,strcmp("",s1.c_str()));
// FORWARD SEARCH
// needle in haystack
idx=s1.find('d');
- CPPUNIT_ASSERT_EQUAL(3,idx);
+ CPPUNIT_ASSERT_EQUAL(3U,idx);
CPPUNIT_ASSERT_EQUAL('d',s1[idx]);
// needle not present in haystack
CPPUNIT_ASSERT_EQUAL(nposResult,idx);
// search in portion
- idx=s1.find('e',3);
- CPPUNIT_ASSERT_EQUAL(4,idx);
+ idx=s1.find('e',3U);
+ CPPUNIT_ASSERT_EQUAL(4U,idx);
// char not in searched portion
- idx=s1.find('e',5);
+ idx=s1.find('e',5U);
CPPUNIT_ASSERT_EQUAL(nposResult,idx);
// invalid start position
idx=s1.find('d',SBuf::npos);
CPPUNIT_ASSERT_EQUAL(nposResult,idx);
- // invalid start position
- idx=s1.find('d', -5);
- CPPUNIT_ASSERT_EQUAL(3, idx);
-
// search outside of haystack
idx=s1.find('d',s1.length()+1);
CPPUNIT_ASSERT_EQUAL(nposResult,idx);
// REVERSE SEARCH
// needle in haystack
idx=s1.rfind('d');
- CPPUNIT_ASSERT_EQUAL(3, idx);
+ CPPUNIT_ASSERT_EQUAL(3U, idx);
CPPUNIT_ASSERT_EQUAL('d', s1[idx]);
// needle not present in haystack
// search in portion
idx=s1.rfind('e',5);
- CPPUNIT_ASSERT_EQUAL(4,idx);
+ CPPUNIT_ASSERT_EQUAL(4U,idx);
// char not in searched portion
idx=s1.rfind('e',3);
CPPUNIT_ASSERT_EQUAL(nposResult,idx);
- // invalid start position
- idx=s1.rfind('d', -5);
- CPPUNIT_ASSERT_EQUAL(nposResult,idx);
-
// overlong haystack specification
idx=s1.rfind('d',s1.length()+1);
- CPPUNIT_ASSERT_EQUAL(3,idx);
+ CPPUNIT_ASSERT_EQUAL(3U,idx);
}
void
// FORWARD search
// needle in haystack
idx = haystack.find(SBuf("def"));
- CPPUNIT_ASSERT_EQUAL(3,idx);
+ CPPUNIT_ASSERT_EQUAL(3U,idx);
idx = haystack.find(SBuf("xyz"));
- CPPUNIT_ASSERT_EQUAL(23,idx);
+ CPPUNIT_ASSERT_EQUAL(23U,idx);
// needle not in haystack, no initial char match
idx = haystack.find(SBuf(" eq"));
idx = haystack.find(SBuf("def"),SBuf::npos);
CPPUNIT_ASSERT_EQUAL(nposResult, idx);
- // invalid start position: negative
- idx = haystack.find(SBuf("def"),-5);
- CPPUNIT_ASSERT_EQUAL(3, idx);
-
// needle bigger than haystack
idx = SBuf("def").find(haystack);
CPPUNIT_ASSERT_EQUAL(nposResult, idx);
h2.append(haystack);
idx = h2.find(SBuf("def"));
- CPPUNIT_ASSERT_EQUAL(3,idx);
+ CPPUNIT_ASSERT_EQUAL(3U,idx);
idx = h2.find(SBuf("xyzab"));
- CPPUNIT_ASSERT_EQUAL(23,idx);
+ CPPUNIT_ASSERT_EQUAL(23U,idx);
}
// REVERSE search
// needle in haystack
idx = haystack.rfind(SBuf("def"));
- CPPUNIT_ASSERT_EQUAL(3,idx);
+ CPPUNIT_ASSERT_EQUAL(3U,idx);
idx = haystack.rfind(SBuf("xyz"));
- CPPUNIT_ASSERT_EQUAL(23,idx);
+ CPPUNIT_ASSERT_EQUAL(23U,idx);
// needle not in haystack, no initial char match
idx = haystack.rfind(SBuf(" eq"));
// search in portion: needle in searched part
idx = haystack.rfind(SBuf("def"),7);
- CPPUNIT_ASSERT_EQUAL(3, idx);
+ CPPUNIT_ASSERT_EQUAL(3U, idx);
// search in portion: needle not in searched part
idx = haystack.rfind(SBuf("mno"),3);
// search in portion: overhang
idx = haystack.rfind(SBuf("def"),4);
- CPPUNIT_ASSERT_EQUAL(3, idx);
+ CPPUNIT_ASSERT_EQUAL(3U, idx);
// npos start position
idx = haystack.rfind(SBuf("def"),SBuf::npos);
- CPPUNIT_ASSERT_EQUAL(3, idx);
-
- // invalid start position: negative
- idx = haystack.rfind(SBuf("def"),-5);
- CPPUNIT_ASSERT_EQUAL(nposResult, idx);
+ CPPUNIT_ASSERT_EQUAL(3U, idx);
// needle bigger than haystack
idx = SBuf("def").rfind(haystack);
h2.append(haystack);
idx = h2.rfind(SBuf("def"));
- CPPUNIT_ASSERT_EQUAL(29,idx);
+ CPPUNIT_ASSERT_EQUAL(29U,idx);
idx = h2.find(SBuf("xyzab"));
- CPPUNIT_ASSERT_EQUAL(23,idx);
+ CPPUNIT_ASSERT_EQUAL(23U,idx);
}
}
SBuf s1(literal);
SBuf::size_type idx;
idx=s1.rfind(' ');
- CPPUNIT_ASSERT_EQUAL(40,idx);
+ CPPUNIT_ASSERT_EQUAL(40U,idx);
CPPUNIT_ASSERT_EQUAL(' ',s1[idx]);
}
CPPUNIT_ASSERT_EQUAL(SBuf::npos,idx);
idx=haystack.rfind(SBuf("fox"));
- CPPUNIT_ASSERT_EQUAL(16,idx);
+ CPPUNIT_ASSERT_EQUAL(16U,idx);
// needle not found, no match for first char
idx=goobar.rfind(SBuf("foo"));
SBuf g("g"); //match at the last char
idx=haystack.rfind(g);
- CPPUNIT_ASSERT_EQUAL(43,idx);
+ CPPUNIT_ASSERT_EQUAL(43U,idx);
CPPUNIT_ASSERT_EQUAL('g',haystack[idx]);
idx=haystack.rfind(SBuf("The"));
- CPPUNIT_ASSERT_EQUAL(0,idx);
+ CPPUNIT_ASSERT_EQUAL(0U,idx);
haystack.append("The");
idx=haystack.rfind(SBuf("The"));
- CPPUNIT_ASSERT_EQUAL(44,idx);
+ CPPUNIT_ASSERT_EQUAL(44U,idx);
//partial match
haystack="The quick brown fox";
CPPUNIT_ASSERT_EQUAL(s.length(),s.copy(buf,40));
CPPUNIT_ASSERT_EQUAL(0,strncmp(s.rawContent(),buf,s.length()));
s=literal;
- CPPUNIT_ASSERT_EQUAL(40,s.copy(buf,40));
+ CPPUNIT_ASSERT_EQUAL(40U,s.copy(buf,40));
s2.assign(buf,40);
s.chop(0,40);
CPPUNIT_ASSERT_EQUAL(s2,s);
// found at beginning
idx=haystack.find_first_of(SBuf("THANDF"));
- CPPUNIT_ASSERT_EQUAL(0,idx);
+ CPPUNIT_ASSERT_EQUAL(0U,idx);
//found at end of haystack
idx=haystack.find_first_of(SBuf("QWERYVg"));
//found in the middle of haystack
idx=haystack.find_first_of(SBuf("QWERqYV"));
- CPPUNIT_ASSERT_EQUAL(4,idx);
+ CPPUNIT_ASSERT_EQUAL(4U,idx);
}
void testSBuf::testAutoFind()