http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/SimpleTokenizer.h ---------------------------------------------------------------------- diff --git a/lib/ts/SimpleTokenizer.h b/lib/ts/SimpleTokenizer.h index 929a606..2a5812a 100644 --- a/lib/ts/SimpleTokenizer.h +++ b/lib/ts/SimpleTokenizer.h @@ -112,12 +112,10 @@ class SimpleTokenizer { public: - // by default, null fields are disregarded, whitespace is trimmed left // and right, and input string is copied (not overwritten) // - enum - { + enum { CONSIDER_NULL_FIELDS = 1, KEEP_WHITESPACE_LEFT = 2, KEEP_WHITESPACE_RIGHT = 4, @@ -127,20 +125,20 @@ public: SimpleTokenizer(char delimiter = ' ', unsigned mode = 0, char escape = '\\') : _data(0), _delimiter(delimiter), _mode(mode), _escape(escape), _start(0), _length(0) - { } + { + } // NOTE: The input strring 's' is overwritten for mode OVERWRITE_INPUT_STRING. SimpleTokenizer(const char *s, char delimiter = ' ', unsigned mode = 0, char escape = '\\') - : _data(0), _delimiter(delimiter), _mode(mode), _escape(escape) + : _data(0), _delimiter(delimiter), _mode(mode), _escape(escape) { setString(s); } - ~SimpleTokenizer() { - _clearData(); - } + ~SimpleTokenizer() { _clearData(); } - void setString(const char *s) + void + setString(const char *s) { _clearData(); @@ -155,69 +153,77 @@ public: // _data[_length++] = _delimiter; }; - char *getNext(int count = 1) { + char * + getNext(int count = 1) + { return _getNext(_delimiter, false, count); }; - char *getNext(char delimiter, int count = 1) { + char * + getNext(char delimiter, int count = 1) + { return _getNext(delimiter, false, count); } - char *getRest() + char * + getRest() { // there can't be more than _length tokens, so we get the rest // of the tokens by requesting _length of them // return _getNext(_delimiter, false, _length); } - size_t getNumTokensRemaining() + size_t + getNumTokensRemaining() { return _getNumTokensRemaining(_delimiter); }; - size_t getNumTokensRemaining(char delimiter) + size_t + getNumTokensRemaining(char delimiter) { return _getNumTokensRemaining(delimiter); }; - char *peekAtRestOfString() + char * + peekAtRestOfString() { _data[_length - 1] = 0; return (_start < _length ? &_data[_start] : &_data[_length - 1]); } private: - - char *_data; // a pointer to the input data itself, + char *_data; // a pointer to the input data itself, // or to a copy of it - char _delimiter; // the token delimiter - unsigned _mode; // flags that determine the + char _delimiter; // the token delimiter + unsigned _mode; // flags that determine the // mode of operation - char _escape; // the escape character - size_t _start; // pointer to the start of the next + char _escape; // the escape character + size_t _start; // pointer to the start of the next // token - size_t _length; // the length of _data + size_t _length; // the length of _data - void _clearData() + void + _clearData() { if (_data && !(_mode & OVERWRITE_INPUT_STRING)) { ats_free(_data); } } - char *_getNext(char delimiter, bool countOnly = false, int numTokens = 1) { + char * + _getNext(char delimiter, bool countOnly = false, int numTokens = 1) + { char *next = NULL; if (_start < _length) { // set start // - bool hasEsc = false; // escape character seen + bool hasEsc = false; // escape character seen while (_start < _length && ((!(_mode & CONSIDER_NULL_FIELDS) && - (_data[_start] == delimiter && - !(_start && - (_data[_start - 1] == _escape ? (hasEsc = true) : 0)))) || + (_data[_start] == delimiter && !(_start && (_data[_start - 1] == _escape ? (hasEsc = true) : 0)))) || (!(_mode & KEEP_WHITESPACE_LEFT) && isspace(_data[_start])))) { ++_start; } - if (_start < _length) // data still available + if (_start < _length) // data still available { // update the extra delimiter just in case the function // is called with a different delimiter from the previous one @@ -230,10 +236,8 @@ private: // size_t end = _start; int delimCount = 0; - while (end < _length && - (_data[end] != delimiter || - (end && (_data[end - 1] == _escape ? (hasEsc = true) : 0)) || - ((++delimCount < numTokens) && (end < _length - 1)))) { + while (end < _length && (_data[end] != delimiter || (end && (_data[end - 1] == _escape ? (hasEsc = true) : 0)) || + ((++delimCount < numTokens) && (end < _length - 1)))) { ++end; } @@ -244,12 +248,14 @@ private: // CONSIDER_NULL_FIELDS flag is not set // if (!(_mode & CONSIDER_NULL_FIELDS)) { - while (_data[--end] == delimiter); + while (_data[--end] == delimiter) + ; ++end; } if (!(_mode & KEEP_WHITESPACE_RIGHT)) { - while (isspace(_data[--end])); + while (isspace(_data[--end])) + ; ++end; } @@ -277,9 +283,10 @@ private: return next; }; - size_t _getNumTokensRemaining(char delimiter) + size_t + _getNumTokensRemaining(char delimiter) { - size_t startSave = _start; // save current position + size_t startSave = _start; // save current position size_t count = 0; while (_getNext(delimiter, true)) { ++count;
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/TestBox.h ---------------------------------------------------------------------- diff --git a/lib/ts/TestBox.h b/lib/ts/TestBox.h index 0461152..cee6926 100644 --- a/lib/ts/TestBox.h +++ b/lib/ts/TestBox.h @@ -1,4 +1,4 @@ -#if ! defined(TS_TEST_BOX_HEADER) +#if !defined(TS_TEST_BOX_HEADER) #define TS_TEST_BOX_HEADER /** @file @@ -24,45 +24,50 @@ limitations under the License. */ -# include <ts/Regression.h> +#include <ts/Regression.h> -namespace { - /** Class to make handling regression tests easier. - This holds the important regression test values so they don't have to - be passed repeated. - */ - struct TestBox { - typedef TestBox self; ///< Self reference type. - RegressionTest* _test; ///< The test object. - int* _status; ///< Current status pointer. +namespace +{ +/** Class to make handling regression tests easier. + This holds the important regression test values so they don't have to + be passed repeated. +*/ +struct TestBox { + typedef TestBox self; ///< Self reference type. + RegressionTest *_test; ///< The test object. + int *_status; ///< Current status pointer. - /// Construct from @a test object and @a status pointer. - TestBox(RegressionTest* test, int* status) : _test(test), _status(status) {} + /// Construct from @a test object and @a status pointer. + TestBox(RegressionTest *test, int *status) : _test(test), _status(status) {} - /// Construct from @a test object, @a status pointer and @a regression status. - TestBox(RegressionTest* test, int* status, int rstatus) : _test(test), _status(status) { - *this = rstatus; - } + /// Construct from @a test object, @a status pointer and @a regression status. + TestBox(RegressionTest *test, int *status, int rstatus) : _test(test), _status(status) { *this = rstatus; } - /// Check the result and print a message on failure. - bool check(bool result, char const* fmt, ...) TS_PRINTFLIKE(3, 4); + /// Check the result and print a message on failure. + bool check(bool result, char const *fmt, ...) TS_PRINTFLIKE(3, 4); - /// Directly assign status. - self& operator = (int status) { *_status = status; return *this; } - }; + /// Directly assign status. + self &operator=(int status) + { + *_status = status; + return *this; + } +}; - bool TestBox::check(bool result, char const* fmt, ...) { - if (!result) { - static size_t const N = 1<<16; - char buffer[N]; // just stack, go big. - va_list ap; - va_start(ap, fmt); - vsnprintf(buffer, N, fmt, ap); - va_end(ap); - rprintf(_test, "%s\n", buffer); - *_status = REGRESSION_TEST_FAILED; - } - return result; +bool +TestBox::check(bool result, char const *fmt, ...) +{ + if (!result) { + static size_t const N = 1 << 16; + char buffer[N]; // just stack, go big. + va_list ap; + va_start(ap, fmt); + vsnprintf(buffer, N, fmt, ap); + va_end(ap); + rprintf(_test, "%s\n", buffer); + *_status = REGRESSION_TEST_FAILED; } + return result; +} } #endif // TS_TEST_BOX_HEADER http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/TestHttpHeader.cc ---------------------------------------------------------------------- diff --git a/lib/ts/TestHttpHeader.cc b/lib/ts/TestHttpHeader.cc index e722d77..3a03bb9 100644 --- a/lib/ts/TestHttpHeader.cc +++ b/lib/ts/TestHttpHeader.cc @@ -48,7 +48,7 @@ #include <sys/stat.h> static void -add_field(HttpHeader * h, const char *name, const char *value) +add_field(HttpHeader *h, const char *name, const char *value) { h->m_header_fields.set_raw_header_field(name, value); } @@ -59,7 +59,7 @@ add_field(HttpHeader * h, const char *name, const char *value) // ///////////////////////////////////////////////////////////// static void -test_add_fields(HttpHeader * h) +test_add_fields(HttpHeader *h) { char long_accept_header[2048]; memset(long_accept_header, 'B', sizeof(long_accept_header)); @@ -130,8 +130,12 @@ void test_url() { test_url_parse("http://charm.example.com "); - test_url_parse - ("http://webchat16.wbs.net:6666?private=herbalessences&color=4&volume=0&tagline=&picture=&home_page=hi@there.&ignore=edheldinruth+taz0069+speezman&back=&Room=Hot_Tub&handle=cagou67&mu=893e159ef7fe0ddb022c655cc1c30abd33d4ae6d90d22f8a&last_read_para=&npo=&fsection=input&chatmode=push&reqtype=input&InputText=Sweetie%2C+do+you+have+time+to+go+to+a+private+room..if+not+I%27m+just+going+to+have+to+change+to+normal+mode...let+me+know%3F%3F/"); + test_url_parse("http://" + "webchat16.wbs.net:6666?private=herbalessences&color=4&volume=0&tagline=&picture=&home_page=hi@there.&ignore=" + "edheldinruth+taz0069+speezman&back=&Room=Hot_Tub&handle=cagou67&mu=" + "893e159ef7fe0ddb022c655cc1c30abd33d4ae6d90d22f8a&last_read_para=&npo=&fsection=input&chatmode=push&reqtype=input&" + "InputText=Sweetie%2C+do+you+have+time+to+go+to+a+private+room..if+not+I%27m+just+going+to+have+to+change+to+" + "normal+mode...let+me+know%3F%3F/"); return; } @@ -161,9 +165,14 @@ test_header_tokenizer_run(const char *buf, HttpMessageType_t message_type) void test_header_tokenizer() { - test_header_tokenizer_run - ("GET http://webchat16.wbs.net:6666?private=herbalessences&color=4&volume=0&tagline=&picture=&home_page=hi@there.&ignore=edheldinruth+taz0069+speezman&back=&Room=Hot_Tub&handle=cagou67&mu=893e159ef7fe0ddb022c655cc1c30abd33d4ae6d90d22f8a&last_read_para=&npo=&fsection=input&chatmode=push&reqtype=input&InputText=Sweetie%2C+do+you+have+time+to+go+to+a+private+room..if+not+I%27m+just+going+to+have+to+change+to+normal+mode...let+me+know%3F%3F/ HTTP/1.0\r\n", - HTTP_MESSAGE_TYPE_REQUEST); + test_header_tokenizer_run("GET " + "http://" + "webchat16.wbs.net:6666?private=herbalessences&color=4&volume=0&tagline=&picture=&home_page=hi@there.&" + "ignore=edheldinruth+taz0069+speezman&back=&Room=Hot_Tub&handle=cagou67&mu=" + "893e159ef7fe0ddb022c655cc1c30abd33d4ae6d90d22f8a&last_read_para=&npo=&fsection=input&chatmode=push&" + "reqtype=input&InputText=Sweetie%2C+do+you+have+time+to+go+to+a+private+room..if+not+I%27m+just+going+" + "to+have+to+change+to+normal+mode...let+me+know%3F%3F/ HTTP/1.0\r\n", + HTTP_MESSAGE_TYPE_REQUEST); return; } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/TextBuffer.cc ---------------------------------------------------------------------- diff --git a/lib/ts/TextBuffer.cc b/lib/ts/TextBuffer.cc index 374892a..b41fbce 100644 --- a/lib/ts/TextBuffer.cc +++ b/lib/ts/TextBuffer.cc @@ -38,7 +38,6 @@ textBuffer::textBuffer(int size) nextAdd = NULL; currentSize = spaceLeft = 0; if (size > 0) { - // Insitute a minimum size if (size < 1024) { size = 1024; @@ -47,7 +46,7 @@ textBuffer::textBuffer(int size) bufferStart = (char *)ats_malloc(size); nextAdd = bufferStart; currentSize = size; - spaceLeft = size - 1; // Leave room for a terminator; + spaceLeft = size - 1; // Leave room for a terminator; nextAdd[0] = '\0'; } } @@ -60,7 +59,7 @@ textBuffer::~textBuffer() char * textBuffer::release() { - char * ret = bufferStart; + char *ret = bufferStart; bufferStart = nextAdd = NULL; currentSize = spaceLeft = 0; @@ -94,7 +93,6 @@ textBuffer::reUse() int textBuffer::copyFrom(const void *source, unsigned num_bytes) { - // Get more space if necessary if (spaceLeft < num_bytes) { if (enlargeBuffer(num_bytes) == -1) { @@ -128,7 +126,6 @@ textBuffer::enlargeBuffer(unsigned N) char *newSpace; if (spaceLeft < N) { - while ((newSize - currentSize) < N) { newSize *= 2; } @@ -137,7 +134,7 @@ textBuffer::enlargeBuffer(unsigned N) newSpace = (char *)ats_realloc(bufferStart, newSize); if (newSpace != NULL) { - nextAdd = newSpace + (unsigned) (nextAdd - bufferStart); + nextAdd = newSpace + (unsigned)(nextAdd - bufferStart); bufferStart = newSpace; spaceLeft += addedSize; currentSize = newSize; @@ -173,7 +170,7 @@ textBuffer::rawReadFromFile(int fd) readSize = read(fd, nextAdd, spaceLeft - 1); - if (readSize == 0) { //EOF + if (readSize == 0) { // EOF return 0; } else if (readSize < 0) { // Error on read @@ -237,7 +234,7 @@ textBuffer::bufPtr() } void -textBuffer::format(const char * fmt, ...) +textBuffer::format(const char *fmt, ...) { va_list ap; bool done = false; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/TextBuffer.h ---------------------------------------------------------------------- diff --git a/lib/ts/TextBuffer.h b/lib/ts/TextBuffer.h index 2dae20b..b9bf0a7 100644 --- a/lib/ts/TextBuffer.h +++ b/lib/ts/TextBuffer.h @@ -39,26 +39,40 @@ class textBuffer { public: inkcoreapi textBuffer(int size); - inkcoreapi ~ textBuffer(); + inkcoreapi ~textBuffer(); int rawReadFromFile(int fd); int readFromFD(int fd); inkcoreapi int copyFrom(const void *, unsigned num_bytes); void reUse(); inkcoreapi char *bufPtr(); - void clear() { this->reUse(); } - void resize(unsigned nbytes) { this->enlargeBuffer(nbytes); } + void + clear() + { + this->reUse(); + } + void + resize(unsigned nbytes) + { + this->enlargeBuffer(nbytes); + } - size_t spaceUsed() const { - return (size_t) (nextAdd - bufferStart); + size_t + spaceUsed() const + { + return (size_t)(nextAdd - bufferStart); }; void chomp(); void slurp(int); - bool empty() const { return this->spaceUsed() == 0; } - void format(const char * fmt, ...) TS_PRINTFLIKE(2, 3); + bool + empty() const + { + return this->spaceUsed() == 0; + } + void format(const char *fmt, ...) TS_PRINTFLIKE(2, 3); - char * release(); + char *release(); private: textBuffer(const textBuffer &); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/Tokenizer.cc ---------------------------------------------------------------------- diff --git a/lib/ts/Tokenizer.cc b/lib/ts/Tokenizer.cc index b4c039d..b2a6f21 100644 --- a/lib/ts/Tokenizer.cc +++ b/lib/ts/Tokenizer.cc @@ -33,7 +33,7 @@ * * * - ****************************************************************************/ /* MAGIC_EDITING_TAG */ + ****************************************************************************/ /* MAGIC_EDITING_TAG */ Tokenizer::Tokenizer(const char *StrOfDelimiters) { @@ -42,7 +42,7 @@ Tokenizer::Tokenizer(const char *StrOfDelimiters) if (StrOfDelimiters == NULL) { strOfDelimit = NULL; } else { - length = (int) (strlen(StrOfDelimiters) + 1); + length = (int)(strlen(StrOfDelimiters) + 1); strOfDelimit = new char[length]; memcpy(strOfDelimit, StrOfDelimiters, length); } @@ -61,15 +61,15 @@ Tokenizer::Tokenizer(const char *StrOfDelimiters) Tokenizer::~Tokenizer() { bool root = true; - tok_node *cur = &start_node;; + tok_node *cur = &start_node; + ; tok_node *next = NULL; if (strOfDelimit != NULL) { - delete[]strOfDelimit; + delete[] strOfDelimit; } while (cur != NULL) { - if (options & COPY_TOKS) { for (int i = 0; i < TOK_NODE_ELEMENTS; i++) ats_free(cur->el[i]); @@ -88,7 +88,7 @@ Tokenizer::~Tokenizer() unsigned Tokenizer::Initialize(const char *str) { - return Initialize((char *) str, COPY_TOKS); + return Initialize((char *)str, COPY_TOKS); } inline int @@ -145,7 +145,6 @@ Tokenizer::Initialize(char *str, unsigned opt) tokStart = str; while (*str != '\0') { - // Check to see if we've run to maxToken limit if (tok_count + 1 == maxTokens) { max_limit_hit = true; @@ -162,7 +161,7 @@ Tokenizer::Initialize(char *str, unsigned opt) // to skip past repeated delimiters if (options & ALLOW_EMPTY_TOKS) { if (isDelimiter(*str)) { - addToken(tokStart, (int) (str - tokStart)); + addToken(tokStart, (int)(str - tokStart)); tok_count++; tokStart = str + 1; priorCharWasDelimit = 1; @@ -174,7 +173,7 @@ Tokenizer::Initialize(char *str, unsigned opt) if (isDelimiter(*str)) { if (priorCharWasDelimit == 0) { // This is a word end, so add it - addToken(tokStart, (int) (str - tokStart)); + addToken(tokStart, (int)(str - tokStart)); tok_count++; } priorCharWasDelimit = 1; @@ -193,19 +192,18 @@ Tokenizer::Initialize(char *str, unsigned opt) // Check to see if we stoped due to a maxToken limit if (max_limit_hit == true) { - if (options & ALLOW_EMPTY_TOKS) { - // Go till either we hit a delimiter or we've // come to the end of the string, then // set for copying - for (; *str != '\0' && !isDelimiter(*str); str++); + for (; *str != '\0' && !isDelimiter(*str); str++) + ; priorCharWasDelimit = 0; } else { - // First, skip the delimiters - for (; *str != '\0' && isDelimiter(*str); str++); + for (; *str != '\0' && isDelimiter(*str); str++) + ; // If there are only delimiters remaining, bail and set // so that we do not add the empty token @@ -217,10 +215,12 @@ Tokenizer::Initialize(char *str, unsigned opt) priorCharWasDelimit = 0; // Advance until the end of the string - for (; *str != '\0'; str++); + for (; *str != '\0'; str++) + ; // Now back off all trailing delimiters - for (; isDelimiter(*(str - 1)); str--); + for (; isDelimiter(*(str - 1)); str--) + ; } } } @@ -230,7 +230,7 @@ Tokenizer::Initialize(char *str, unsigned opt) // only have gotten it if the string ended with a delimiter if (priorCharWasDelimit == 0) { // We did not get it - addToken(tokStart, (int) (str - tokStart)); + addToken(tokStart, (int)(str - tokStart)); tok_count++; } @@ -271,10 +271,9 @@ Tokenizer::addToken(char *startAddr, int length) } -const char * -Tokenizer::operator[] (unsigned index) const +const char *Tokenizer::operator[](unsigned index) const { - const tok_node * cur_node = &start_node; + const tok_node *cur_node = &start_node; unsigned cur_start = 0; if (index >= numValidTokens) { @@ -296,7 +295,7 @@ Tokenizer::count() const } const char * -Tokenizer::iterFirst(tok_iter_state * state) +Tokenizer::iterFirst(tok_iter_state *state) { state->node = &start_node; state->index = -1; @@ -304,9 +303,10 @@ Tokenizer::iterFirst(tok_iter_state * state) } const char * -Tokenizer::iterNext(tok_iter_state * state) +Tokenizer::iterNext(tok_iter_state *state) { - tok_node *node = state->node;; + tok_node *node = state->node; + ; int index = state->index; index++; @@ -329,7 +329,6 @@ Tokenizer::iterNext(tok_iter_state * state) } - void Tokenizer::Print() { @@ -338,7 +337,6 @@ Tokenizer::Print() int count = 0; while (cur_node != NULL) { - if (cur_node->el[node_index] != NULL) { printf("Token %d : |%s|\n", count, cur_node->el[node_index]); count++; @@ -376,22 +374,16 @@ Tokenizer::ReUse() #if TS_HAS_TESTS #include "TestBox.h" -REGRESSION_TEST(libts_Tokenizer) (RegressionTest * test, int /* atype ATS_UNUSED */, int *pstatus) +REGRESSION_TEST(libts_Tokenizer)(RegressionTest *test, int /* atype ATS_UNUSED */, int *pstatus) { TestBox box(test, pstatus); box = REGRESSION_TEST_PASSED; Tokenizer remap(" \t"); - const char * line = "map https://abc.com https://abc.com @plugin=conf_remap.so @pparam=proxy.config.abc='ABC DEF'"; + const char *line = "map https://abc.com https://abc.com @plugin=conf_remap.so @pparam=proxy.config.abc='ABC DEF'"; - const char * toks[] = { - "map", - "https://abc.com", - "https://abc.com", - "@plugin=conf_remap.so", - "@pparam=proxy.config.abc='ABC DEF'" - }; + const char *toks[] = {"map", "https://abc.com", "https://abc.com", "@plugin=conf_remap.so", "@pparam=proxy.config.abc='ABC DEF'"}; unsigned count = remap.Initialize(const_cast<char *>(line), (COPY_TOKS | ALLOW_SPACES)); @@ -399,8 +391,7 @@ REGRESSION_TEST(libts_Tokenizer) (RegressionTest * test, int /* atype ATS_UNUSED box.check(count == remap.count(), "parsed %u tokens, but now we have %u tokens", count, remap.count()); for (unsigned i = 0; i < count; ++i) { - box.check(strcmp(remap[i], toks[i]) == 0, "expected token %u to be '%s' but found '%s'", - count, toks[i], remap[i]); + box.check(strcmp(remap[i], toks[i]) == 0, "expected token %u to be '%s' but found '%s'", count, toks[i], remap[i]); } } #endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/Tokenizer.h ---------------------------------------------------------------------- diff --git a/lib/ts/Tokenizer.h b/lib/ts/Tokenizer.h index 81ac7cf..8157458 100644 --- a/lib/ts/Tokenizer.h +++ b/lib/ts/Tokenizer.h @@ -104,21 +104,19 @@ #include "ink_apidefs.h" -#define COPY_TOKS (1u << 0) -#define SHARE_TOKS (1u << 1) -#define ALLOW_EMPTY_TOKS (1u << 2) -#define ALLOW_SPACES (1u << 3) +#define COPY_TOKS (1u << 0) +#define SHARE_TOKS (1u << 1) +#define ALLOW_EMPTY_TOKS (1u << 2) +#define ALLOW_SPACES (1u << 3) -#define TOK_NODE_ELEMENTS 16 +#define TOK_NODE_ELEMENTS 16 -struct tok_node -{ +struct tok_node { char *el[TOK_NODE_ELEMENTS]; tok_node *next; }; -struct tok_iter_state -{ +struct tok_iter_state { tok_node *node; int index; }; @@ -130,25 +128,29 @@ public: inkcoreapi ~Tokenizer(); unsigned Initialize(char *str, unsigned options); - inkcoreapi unsigned Initialize(const char *str); // Automatically sets option to copy - const char * operator[] (unsigned index) const; + inkcoreapi unsigned Initialize(const char *str); // Automatically sets option to copy + const char *operator[](unsigned index) const; - void setMaxTokens(unsigned max) { + void + setMaxTokens(unsigned max) + { maxTokens = max; }; - unsigned getMaxTokens() const { + unsigned + getMaxTokens() const + { return maxTokens; }; unsigned count() const; - void Print(); // Debugging print out + void Print(); // Debugging print out - inkcoreapi const char *iterFirst(tok_iter_state * state); - inkcoreapi const char *iterNext(tok_iter_state * state); + inkcoreapi const char *iterFirst(tok_iter_state *state); + inkcoreapi const char *iterNext(tok_iter_state *state); private: - Tokenizer & operator=(const Tokenizer &); + Tokenizer &operator=(const Tokenizer &); Tokenizer(const Tokenizer &); int isDelimiter(char c); void addToken(char *startAddr, int length); http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/Trie.h ---------------------------------------------------------------------- diff --git a/lib/ts/Trie.h b/lib/ts/Trie.h index 5bf766d..0a8fa79 100644 --- a/lib/ts/Trie.h +++ b/lib/ts/Trie.h @@ -31,25 +31,25 @@ // Note that you should provide the class to use here, but we'll store // pointers to such objects internally. -template<typename T> -class Trie +template <typename T> class Trie { public: - Trie() - { - m_root.Clear(); - } + Trie() { m_root.Clear(); } // will return false for duplicates; key should be NULL-terminated // if key_len is defaulted to -1 bool Insert(const char *key, T *value, int rank, int key_len = -1); // will return false if not found; else value_ptr will point to found value - T* Search(const char *key, int key_len = -1) const; + T *Search(const char *key, int key_len = -1) const; void Clear(); void Print(); - bool Empty() const { return m_value_list.empty(); } + bool + Empty() const + { + return m_value_list.empty(); + } virtual ~Trie() { Clear(); } @@ -59,11 +59,13 @@ private: class Node { public: - T* value; + T *value; bool occupied; int rank; - void Clear() { + void + Clear() + { value = NULL; occupied = false; rank = 0; @@ -71,11 +73,17 @@ private: } void Print(const char *debug_tag) const; - inline Node* GetChild(char index) const { return children[static_cast<unsigned char>(index)]; } - inline Node* AllocateChild(char index) { - Node * &child = children[static_cast<unsigned char>(index)]; + inline Node * + GetChild(char index) const + { + return children[static_cast<unsigned char>(index)]; + } + inline Node * + AllocateChild(char index) + { + Node *&child = children[static_cast<unsigned char>(index)]; ink_assert(child == NULL); - child = static_cast<Node*>(ats_malloc(sizeof(Node))); + child = static_cast<Node *>(ats_malloc(sizeof(Node))); child->Clear(); return child; } @@ -92,25 +100,24 @@ private: // make copy-constructor and assignment operator private // till we properly implement them - Trie(const Trie<T> &rhs) { }; - Trie &operator =(const Trie<T> &rhs) { return *this; } + Trie(const Trie<T> &rhs){}; + Trie &operator=(const Trie<T> &rhs) { return *this; } }; -template<typename T> +template <typename T> void Trie<T>::_CheckArgs(const char *key, int &key_len) const { if (!key) { key_len = 0; - } - else if (key_len == -1) { + } else if (key_len == -1) { key_len = strlen(key); } } -template<typename T> +template <typename T> bool -Trie<T>::Insert(const char *key, T* value, int rank, int key_len /* = -1 */) +Trie<T>::Insert(const char *key, T *value, int rank, int key_len /* = -1 */) { _CheckArgs(key, key_len); @@ -154,8 +161,8 @@ Trie<T>::Insert(const char *key, T* value, int rank, int key_len /* = -1 */) return true; } -template<typename T> -T* +template <typename T> +T * Trie<T>::Search(const char *key, int key_len /* = -1 */) const { _CheckArgs(key, key_len); @@ -190,7 +197,7 @@ Trie<T>::Search(const char *key, int key_len /* = -1 */) const } -template<typename T> +template <typename T> void Trie<T>::_Clear(Node *node) { @@ -205,11 +212,11 @@ Trie<T>::_Clear(Node *node) } } -template<typename T> +template <typename T> void Trie<T>::Clear() { - T* iter; + T *iter; while (NULL != (iter = m_value_list.pop())) delete iter; @@ -217,15 +224,15 @@ Trie<T>::Clear() m_root.Clear(); } -template<typename T> +template <typename T> void -Trie<T>::Print() { +Trie<T>::Print() +{ // The class we contain must provide a ::Print() method. - forl_LL(T, iter, m_value_list) - iter->Print(); + forl_LL(T, iter, m_value_list) iter->Print(); } -template<typename T> +template <typename T> void Trie<T>::Node::Print(const char *debug_tag) const { http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/TsBuffer.h ---------------------------------------------------------------------- diff --git a/lib/ts/TsBuffer.h b/lib/ts/TsBuffer.h index 006c821..4da12ff 100644 --- a/lib/ts/TsBuffer.h +++ b/lib/ts/TsBuffer.h @@ -1,5 +1,5 @@ -# if ! defined TS_BUFFER_HEADER -# define TS_BUFFER_HEADER +#if !defined TS_BUFFER_HEADER +#define TS_BUFFER_HEADER /** @file Definitions for a buffer type, to carry a reference to a chunk of memory. @@ -26,376 +26,483 @@ limitations under the License. */ -# if defined _MSC_VER -# include <stddef.h> -# else -# include <unistd.h> -# endif +#if defined _MSC_VER +#include <stddef.h> +#else +#include <unistd.h> +#endif // For memcmp() -# include <memory.h> +#include <memory.h> /// Apache Traffic Server commons. -namespace ts { - struct ConstBuffer; - /** A chunk of writable memory. - A convenience class because we pass this kind of pair frequently. - - @note The default construct leaves the object - uninitialized. This is for performance reasons. To construct an - empty @c Buffer use @c Buffer(0). - */ - struct Buffer { - typedef Buffer self; ///< Self reference type. - typedef bool (self::*pseudo_bool)() const; - - char * _ptr; ///< Pointer to base of memory chunk. - size_t _size; ///< Size of memory chunk. - - /// Default constructor (empty buffer). - Buffer(); - - /** Construct from pointer and size. - @note Due to ambiguity issues do not call this with - two arguments if the first argument is 0. - */ - Buffer( - char* ptr, ///< Pointer to buffer. - size_t n ///< Size of buffer. - ); - /** Construct from two pointers. - @note This presumes a half open range, (start, end] - */ - Buffer( - char* start, ///< First valid character. - char* end ///< First invalid character. - ); - - /** Equality. - @return @c true if @a that refers to the same memory as @a this, - @c false otherwise. - */ - bool operator == (self const& that) const; - /** Inequality. - @return @c true if @a that does not refer to the same memory as @a this, - @c false otherwise. - */ - bool operator != (self const& that) const; - /** Equality for a constant buffer. - @return @c true if @a that refers to the same memory as @a this. - @c false otherwise. - */ - bool operator == (ConstBuffer const& that) const; - /** Inequality. - @return @c true if @a that does not refer to the same memory as @a this, - @c false otherwise. - */ - bool operator != (ConstBuffer const& that) const; - - /// @return The first character in the buffer. - char operator* () const; - /** Discard the first character in the buffer. - @return @a this object. - */ - self& operator++(); - - /// Check for empty buffer. - /// @return @c true if the buffer has a zero pointer @b or size. - bool operator ! () const; - /// Check for non-empty buffer. - /// @return @c true if the buffer has a non-zero pointer @b and size. - operator pseudo_bool() const; - - /// @name Accessors. - //@{ - /// Get the data in the buffer. - char* data() const; - /// Get the size of the buffer. - size_t size() const; - //@} - - /// Set the chunk. - /// Any previous values are discarded. - /// @return @c this object. - self& set( - char* ptr, ///< Buffer address. - size_t n = 0 ///< Buffer size. - ); - /// Reset to empty. - self& reset(); - }; - - /** A chunk of read only memory. - A convenience class because we pass this kind of pair frequently. - */ - struct ConstBuffer { - typedef ConstBuffer self; ///< Self reference type. - typedef bool (self::*pseudo_bool)() const; - - char const * _ptr; ///< Pointer to base of memory chunk. - size_t _size; ///< Size of memory chunk. - - /// Default constructor (empty buffer). - ConstBuffer(); - - /** Construct from pointer and size. - */ - ConstBuffer( - char const * ptr, ///< Pointer to buffer. - size_t n ///< Size of buffer. - ); - /** Construct from two pointers. - @note This presumes a half open range (start, end] - @note Due to ambiguity issues do not invoke this with - @a start == 0. - */ - ConstBuffer( - char const* start, ///< First valid character. - char const* end ///< First invalid character. - ); - /// Construct from writable buffer. - ConstBuffer( - Buffer const& buffer ///< Buffer to copy. - ); - - /** Equality. - @return @c true if @a that refers to the same memory as @a this, - @c false otherwise. - */ - bool operator == (self const& that) const; - /** Equality. - @return @c true if @a that refers to the same memory as @a this, - @c false otherwise. - */ - bool operator == (Buffer const& that) const; - /** Inequality. - @return @c true if @a that does not refer to the same memory as @a this, - @c false otherwise. - */ - bool operator != (self const& that) const; - /** Inequality. - @return @c true if @a that does not refer to the same memory as @a this, - @c false otherwise. - */ - bool operator != (Buffer const& that) const; - /// Assign from non-const Buffer. - self& operator = ( - Buffer const& that ///< Source buffer. - ); - - /// @return The first character in the buffer. - char operator* () const; - /** Discard the first character in the buffer. - @return @a this object. - */ - self& operator++(); - /** Discard the first @a n characters. - @return @a this object. - */ - self& operator += (size_t n); - - /// Check for empty buffer. - /// @return @c true if the buffer has a zero pointer @b or size. - bool operator ! () const; - /// Check for non-empty buffer. - /// @return @c true if the buffer has a non-zero pointer @b and size. - operator pseudo_bool() const; - - /// @name Accessors. - //@{ - /// Get the data in the buffer. - char const * data() const; - /// Get the size of the buffer. - size_t size() const; - /// Access a character (no bounds check). - char operator[] (int n) const; - //@} - /// @return @c true if @a p points at a character in @a this. - bool contains(char const* p) const; - - /// Set the chunk. - /// Any previous values are discarded. - /// @return @c this object. - self& set( - char const * ptr, ///< Buffer address. - size_t n = 0 ///< Buffer size. - ); - /** Set from 2 pointers. - @note This presumes a half open range (start, end] - */ - self& set( - char const* start, ///< First valid character. - char const* end ///< First invalid character. - ); - /// Reset to empty. - self& reset(); - - /** Find a character. - @return A pointer to the first occurrence of @a c in @a this - or @c NULL if @a c is not found. - */ - char const* find(char c) const; - - /** Split the buffer on the character at @a p. - - The buffer is split in to two parts and the character at @a p - is discarded. @a this retains all data @b after @a p. The - initial part of the buffer is returned. Neither buffer will - contain the character at @a p. - - This is convenient when tokenizing and @a p points at the token - separator. - - @note If @a *p is not in the buffer then @a this is not changed - and an empty buffer is returned. This means the caller can - simply pass the result of @c find and check for an empty - buffer returned to detect no more separators. - - @return A buffer containing data up to but not including @a p. - */ - self splitOn(char const* p); - - /** Split the buffer on the character @a c. - - The buffer is split in to two parts and the occurrence of @a c - is discarded. @a this retains all data @b after @a c. The - initial part of the buffer is returned. Neither buffer will - contain the first occurrence of @a c. - - This is convenient when tokenizing and @a c is the token - separator. - - @note If @a c is not found then @a this is not changed and an - empty buffer is returned. - - @return A buffer containing data up to but not including @a p. - */ - self splitOn(char c); - /** Get a trailing segment of the buffer. - - @return A buffer that contains all data after @a p. - */ - self after(char const* p) const; - /** Get a trailing segment of the buffer. - - @return A buffer that contains all data after the first - occurrence of @a c. - */ - self after(char c) const; - /** Remove trailing segment. - - Data at @a p and beyond is removed from the buffer. - If @a p is not in the buffer, no change is made. - - @return @a this. - */ - self& clip(char const* p); - }; - - // ---------------------------------------------------------- - // Inline implementations. - - inline Buffer::Buffer() : _ptr(NULL), _size(0) { } - inline Buffer::Buffer(char* ptr, size_t n) : _ptr(ptr), _size(n) { } - inline Buffer& Buffer::set(char* ptr, size_t n) { _ptr = ptr; _size = n; return *this; } - inline Buffer::Buffer(char* start, char* end) : _ptr(start), _size(end - start) { } - inline Buffer& Buffer::reset() { _ptr = 0; _size = 0 ; return *this; } - inline bool Buffer::operator != (self const& that) const { return ! (*this == that); } - inline bool Buffer::operator != (ConstBuffer const& that) const { return ! (*this == that); } - inline bool Buffer::operator == (self const& that) const { - return _size == that._size && _ptr == that._ptr; - } - inline bool Buffer::operator == (ConstBuffer const& that) const { - return _size == that._size && _ptr == that._ptr; - } - inline bool Buffer::operator ! () const { return !(_ptr && _size); } - inline Buffer::operator pseudo_bool() const { return _ptr && _size ? &self::operator! : 0; } - inline char Buffer::operator * () const { return *_ptr; } - inline Buffer& Buffer::operator++ () { - ++_ptr; - --_size; - return *this; - } - inline char * Buffer::data() const { return _ptr; } - inline size_t Buffer::size() const { return _size; } - - inline ConstBuffer::ConstBuffer() : _ptr(NULL), _size(0) { } - inline ConstBuffer::ConstBuffer(char const* ptr, size_t n) : _ptr(ptr), _size(n) { } - inline ConstBuffer::ConstBuffer(char const* start, char const* end) : _ptr(start), _size(end - start) { } - inline ConstBuffer::ConstBuffer(Buffer const& that) : _ptr(that._ptr), _size(that._size) { } - inline ConstBuffer& ConstBuffer::set(char const* ptr, size_t n) { _ptr = ptr; _size = n; return *this; } - - inline ConstBuffer& ConstBuffer::set(char const* start, char const* end) { - _ptr = start; - _size = end - start; - return *this; - } +namespace ts +{ +struct ConstBuffer; +/** A chunk of writable memory. + A convenience class because we pass this kind of pair frequently. + + @note The default construct leaves the object + uninitialized. This is for performance reasons. To construct an + empty @c Buffer use @c Buffer(0). + */ +struct Buffer { + typedef Buffer self; ///< Self reference type. + typedef bool (self::*pseudo_bool)() const; - inline ConstBuffer& ConstBuffer::reset() { _ptr = 0; _size = 0 ; return *this; } - inline bool ConstBuffer::operator != (self const& that) const { return ! (*this == that); } - inline bool ConstBuffer::operator != (Buffer const& that) const { return ! (*this == that); } - inline bool ConstBuffer::operator == (self const& that) const { - return _size == that._size && 0 == memcmp(_ptr, that._ptr, _size); - } - inline ConstBuffer& ConstBuffer::operator = (Buffer const& that) { _ptr = that._ptr ; _size = that._size; return *this; } - inline bool ConstBuffer::operator == (Buffer const& that) const { - return _size == that._size && 0 == memcmp(_ptr, that._ptr, _size); - } - inline bool ConstBuffer::operator ! () const { return !(_ptr && _size); } - inline ConstBuffer::operator pseudo_bool() const { return _ptr && _size ? &self::operator! : 0; } - inline char ConstBuffer::operator * () const { return *_ptr; } - inline ConstBuffer& ConstBuffer::operator++ () { - ++_ptr; - --_size; - return *this; - } - inline ConstBuffer& ConstBuffer::operator += (size_t n) { - _ptr += n; - _size -= n; - return *this; - } - inline char const * ConstBuffer::data() const { return _ptr; } - inline char ConstBuffer::operator[] (int n) const { return _ptr[n]; } - inline size_t ConstBuffer::size() const { return _size; } - inline bool ConstBuffer::contains(char const* p) const { - return _ptr <= p && p < _ptr + _size; - } + char *_ptr; ///< Pointer to base of memory chunk. + size_t _size; ///< Size of memory chunk. - inline ConstBuffer ConstBuffer::splitOn(char const* p) { - self zret; // default to empty return. - if (this->contains(p)) { - size_t n = p - _ptr; - zret.set(_ptr, n); - _ptr = p + 1; - _size -= n + 1; - } - return zret; - } + /// Default constructor (empty buffer). + Buffer(); - inline char const* ConstBuffer::find(char c) const { - return static_cast<char const*>(memchr(_ptr, c, _size)); - } + /** Construct from pointer and size. + @note Due to ambiguity issues do not call this with + two arguments if the first argument is 0. + */ + Buffer(char *ptr, ///< Pointer to buffer. + size_t n ///< Size of buffer. + ); + /** Construct from two pointers. + @note This presumes a half open range, (start, end] + */ + Buffer(char *start, ///< First valid character. + char *end ///< First invalid character. + ); + + /** Equality. + @return @c true if @a that refers to the same memory as @a this, + @c false otherwise. + */ + bool operator==(self const &that) const; + /** Inequality. + @return @c true if @a that does not refer to the same memory as @a this, + @c false otherwise. + */ + bool operator!=(self const &that) const; + /** Equality for a constant buffer. + @return @c true if @a that refers to the same memory as @a this. + @c false otherwise. + */ + bool operator==(ConstBuffer const &that) const; + /** Inequality. + @return @c true if @a that does not refer to the same memory as @a this, + @c false otherwise. + */ + bool operator!=(ConstBuffer const &that) const; + + /// @return The first character in the buffer. + char operator*() const; + /** Discard the first character in the buffer. + @return @a this object. + */ + self &operator++(); + + /// Check for empty buffer. + /// @return @c true if the buffer has a zero pointer @b or size. + bool operator!() const; + /// Check for non-empty buffer. + /// @return @c true if the buffer has a non-zero pointer @b and size. + operator pseudo_bool() const; + + /// @name Accessors. + //@{ + /// Get the data in the buffer. + char *data() const; + /// Get the size of the buffer. + size_t size() const; + //@} + + /// Set the chunk. + /// Any previous values are discarded. + /// @return @c this object. + self &set(char *ptr, ///< Buffer address. + size_t n = 0 ///< Buffer size. + ); + /// Reset to empty. + self &reset(); +}; + +/** A chunk of read only memory. + A convenience class because we pass this kind of pair frequently. + */ +struct ConstBuffer { + typedef ConstBuffer self; ///< Self reference type. + typedef bool (self::*pseudo_bool)() const; - inline ConstBuffer ConstBuffer::splitOn(char c) { - return this->splitOn(this->find(c)); - } + char const *_ptr; ///< Pointer to base of memory chunk. + size_t _size; ///< Size of memory chunk. - inline ConstBuffer ConstBuffer::after(char const* p) const { - return this->contains(p) ? self(p + 1, (_size-(p-_ptr))-1) : self(); - } - inline ConstBuffer ConstBuffer::after(char c) const { - return this->after(this->find(c)); + /// Default constructor (empty buffer). + ConstBuffer(); + + /** Construct from pointer and size. + */ + ConstBuffer(char const *ptr, ///< Pointer to buffer. + size_t n ///< Size of buffer. + ); + /** Construct from two pointers. + @note This presumes a half open range (start, end] + @note Due to ambiguity issues do not invoke this with + @a start == 0. + */ + ConstBuffer(char const *start, ///< First valid character. + char const *end ///< First invalid character. + ); + /// Construct from writable buffer. + ConstBuffer(Buffer const &buffer ///< Buffer to copy. + ); + + /** Equality. + @return @c true if @a that refers to the same memory as @a this, + @c false otherwise. + */ + bool operator==(self const &that) const; + /** Equality. + @return @c true if @a that refers to the same memory as @a this, + @c false otherwise. + */ + bool operator==(Buffer const &that) const; + /** Inequality. + @return @c true if @a that does not refer to the same memory as @a this, + @c false otherwise. + */ + bool operator!=(self const &that) const; + /** Inequality. + @return @c true if @a that does not refer to the same memory as @a this, + @c false otherwise. + */ + bool operator!=(Buffer const &that) const; + /// Assign from non-const Buffer. + self &operator=(Buffer const &that ///< Source buffer. + ); + + /// @return The first character in the buffer. + char operator*() const; + /** Discard the first character in the buffer. + @return @a this object. + */ + self &operator++(); + /** Discard the first @a n characters. + @return @a this object. + */ + self &operator+=(size_t n); + + /// Check for empty buffer. + /// @return @c true if the buffer has a zero pointer @b or size. + bool operator!() const; + /// Check for non-empty buffer. + /// @return @c true if the buffer has a non-zero pointer @b and size. + operator pseudo_bool() const; + + /// @name Accessors. + //@{ + /// Get the data in the buffer. + char const *data() const; + /// Get the size of the buffer. + size_t size() const; + /// Access a character (no bounds check). + char operator[](int n) const; + //@} + /// @return @c true if @a p points at a character in @a this. + bool contains(char const *p) const; + + /// Set the chunk. + /// Any previous values are discarded. + /// @return @c this object. + self &set(char const *ptr, ///< Buffer address. + size_t n = 0 ///< Buffer size. + ); + /** Set from 2 pointers. + @note This presumes a half open range (start, end] + */ + self &set(char const *start, ///< First valid character. + char const *end ///< First invalid character. + ); + /// Reset to empty. + self &reset(); + + /** Find a character. + @return A pointer to the first occurrence of @a c in @a this + or @c NULL if @a c is not found. + */ + char const *find(char c) const; + + /** Split the buffer on the character at @a p. + + The buffer is split in to two parts and the character at @a p + is discarded. @a this retains all data @b after @a p. The + initial part of the buffer is returned. Neither buffer will + contain the character at @a p. + + This is convenient when tokenizing and @a p points at the token + separator. + + @note If @a *p is not in the buffer then @a this is not changed + and an empty buffer is returned. This means the caller can + simply pass the result of @c find and check for an empty + buffer returned to detect no more separators. + + @return A buffer containing data up to but not including @a p. + */ + self splitOn(char const *p); + + /** Split the buffer on the character @a c. + + The buffer is split in to two parts and the occurrence of @a c + is discarded. @a this retains all data @b after @a c. The + initial part of the buffer is returned. Neither buffer will + contain the first occurrence of @a c. + + This is convenient when tokenizing and @a c is the token + separator. + + @note If @a c is not found then @a this is not changed and an + empty buffer is returned. + + @return A buffer containing data up to but not including @a p. + */ + self splitOn(char c); + /** Get a trailing segment of the buffer. + + @return A buffer that contains all data after @a p. + */ + self after(char const *p) const; + /** Get a trailing segment of the buffer. + + @return A buffer that contains all data after the first + occurrence of @a c. + */ + self after(char c) const; + /** Remove trailing segment. + + Data at @a p and beyond is removed from the buffer. + If @a p is not in the buffer, no change is made. + + @return @a this. + */ + self &clip(char const *p); +}; + +// ---------------------------------------------------------- +// Inline implementations. + +inline Buffer::Buffer() : _ptr(NULL), _size(0) +{ +} +inline Buffer::Buffer(char *ptr, size_t n) : _ptr(ptr), _size(n) +{ +} +inline Buffer & +Buffer::set(char *ptr, size_t n) +{ + _ptr = ptr; + _size = n; + return *this; +} +inline Buffer::Buffer(char *start, char *end) : _ptr(start), _size(end - start) +{ +} +inline Buffer & +Buffer::reset() +{ + _ptr = 0; + _size = 0; + return *this; +} +inline bool Buffer::operator!=(self const &that) const +{ + return !(*this == that); +} +inline bool Buffer::operator!=(ConstBuffer const &that) const +{ + return !(*this == that); +} +inline bool Buffer::operator==(self const &that) const +{ + return _size == that._size && _ptr == that._ptr; +} +inline bool Buffer::operator==(ConstBuffer const &that) const +{ + return _size == that._size && _ptr == that._ptr; +} +inline bool Buffer::operator!() const +{ + return !(_ptr && _size); +} +inline Buffer::operator pseudo_bool() const +{ + return _ptr && _size ? &self::operator! : 0; +} +inline char Buffer::operator*() const +{ + return *_ptr; +} +inline Buffer &Buffer::operator++() +{ + ++_ptr; + --_size; + return *this; +} +inline char * +Buffer::data() const +{ + return _ptr; +} +inline size_t +Buffer::size() const +{ + return _size; +} + +inline ConstBuffer::ConstBuffer() : _ptr(NULL), _size(0) +{ +} +inline ConstBuffer::ConstBuffer(char const *ptr, size_t n) : _ptr(ptr), _size(n) +{ +} +inline ConstBuffer::ConstBuffer(char const *start, char const *end) : _ptr(start), _size(end - start) +{ +} +inline ConstBuffer::ConstBuffer(Buffer const &that) : _ptr(that._ptr), _size(that._size) +{ +} +inline ConstBuffer & +ConstBuffer::set(char const *ptr, size_t n) +{ + _ptr = ptr; + _size = n; + return *this; +} + +inline ConstBuffer & +ConstBuffer::set(char const *start, char const *end) +{ + _ptr = start; + _size = end - start; + return *this; +} + +inline ConstBuffer & +ConstBuffer::reset() +{ + _ptr = 0; + _size = 0; + return *this; +} +inline bool ConstBuffer::operator!=(self const &that) const +{ + return !(*this == that); +} +inline bool ConstBuffer::operator!=(Buffer const &that) const +{ + return !(*this == that); +} +inline bool ConstBuffer::operator==(self const &that) const +{ + return _size == that._size && 0 == memcmp(_ptr, that._ptr, _size); +} +inline ConstBuffer &ConstBuffer::operator=(Buffer const &that) +{ + _ptr = that._ptr; + _size = that._size; + return *this; +} +inline bool ConstBuffer::operator==(Buffer const &that) const +{ + return _size == that._size && 0 == memcmp(_ptr, that._ptr, _size); +} +inline bool ConstBuffer::operator!() const +{ + return !(_ptr && _size); +} +inline ConstBuffer::operator pseudo_bool() const +{ + return _ptr && _size ? &self::operator! : 0; +} +inline char ConstBuffer::operator*() const +{ + return *_ptr; +} +inline ConstBuffer &ConstBuffer::operator++() +{ + ++_ptr; + --_size; + return *this; +} +inline ConstBuffer &ConstBuffer::operator+=(size_t n) +{ + _ptr += n; + _size -= n; + return *this; +} +inline char const * +ConstBuffer::data() const +{ + return _ptr; +} +inline char ConstBuffer::operator[](int n) const +{ + return _ptr[n]; +} +inline size_t +ConstBuffer::size() const +{ + return _size; +} +inline bool +ConstBuffer::contains(char const *p) const +{ + return _ptr <= p && p < _ptr + _size; +} + +inline ConstBuffer +ConstBuffer::splitOn(char const *p) +{ + self zret; // default to empty return. + if (this->contains(p)) { + size_t n = p - _ptr; + zret.set(_ptr, n); + _ptr = p + 1; + _size -= n + 1; } - inline ConstBuffer& ConstBuffer::clip(char const* p) { - if (this->contains(p)) { - _size = p - _ptr; - } - return *this; + return zret; +} + +inline char const * +ConstBuffer::find(char c) const +{ + return static_cast<char const *>(memchr(_ptr, c, _size)); +} + +inline ConstBuffer +ConstBuffer::splitOn(char c) +{ + return this->splitOn(this->find(c)); +} + +inline ConstBuffer +ConstBuffer::after(char const *p) const +{ + return this->contains(p) ? self(p + 1, (_size - (p - _ptr)) - 1) : self(); +} +inline ConstBuffer +ConstBuffer::after(char c) const +{ + return this->after(this->find(c)); +} +inline ConstBuffer & +ConstBuffer::clip(char const *p) +{ + if (this->contains(p)) { + _size = p - _ptr; } + return *this; +} } // end namespace typedef ts::Buffer TsBuffer; typedef ts::ConstBuffer TsConstBuffer; -# endif // TS_BUFFER_HEADER +#endif // TS_BUFFER_HEADER http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/Vec.cc ---------------------------------------------------------------------- diff --git a/lib/ts/Vec.cc b/lib/ts/Vec.cc index 781e7dc..669d413 100644 --- a/lib/ts/Vec.cc +++ b/lib/ts/Vec.cc @@ -25,55 +25,46 @@ #include <stdint.h> #include "Vec.h" -const uintptr_t prime2[] = { - 1, 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, - 16381, 32749, 65521, 131071, 262139, 524287, 1048573, 2097143, - 4194301, 8388593, 16777213, 33554393, 67108859, 134217689, - 268435399, 536870909 -}; +const uintptr_t prime2[] = {1, 3, 7, 13, 31, 61, 127, 251, 509, 1021, + 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139, 524287, 1048573, + 2097143, 4194301, 8388593, 16777213, 33554393, 67108859, 134217689, 268435399, 536870909}; // primes generated with map_mult.c const uintptr_t open_hash_primes[256] = { -0x02D4AF27, 0x1865DFC7, 0x47C62B43, 0x35B4889B, 0x210459A1, 0x3CC51CC7, 0x02ADD945, 0x0607C4D7, -0x558E6035, 0x0554224F, 0x5A281657, 0x1C458C7F, 0x7F8BE723, 0x20B9BA99, 0x7218AA35, 0x64B10C2B, -0x548E8983, 0x5951218F, 0x7AADC871, 0x695FA5B1, 0x40D40FCB, 0x20E03CC9, 0x55E9920F, 0x554CE08B, -0x7E78B1D7, 0x7D965DF9, 0x36A520A1, 0x1B0C6C11, 0x33385667, 0x2B0A7B9B, 0x0F35AE23, 0x0BD608FB, -0x2284ADA3, 0x6E6C0687, 0x129B3EED, 0x7E86289D, 0x1143C24B, 0x1B6C7711, 0x1D87BB41, 0x4C7E635D, -0x67577999, 0x0A0113C5, 0x6CF085B5, 0x14A4D0FB, 0x4E93E3A7, 0x5C87672B, 0x67F3CA17, 0x5F944339, -0x4C16DFD7, 0x5310C0E3, 0x2FAD1447, 0x4AFB3187, 0x08468B7F, 0x49E56C51, 0x6280012F, 0x097D1A85, -0x34CC9403, 0x71028BD7, 0x6DEDC7E9, 0x64093291, 0x6D78BB0B, 0x7A03B465, 0x2E044A43, 0x1AE58515, -0x23E495CD, 0x46102A83, 0x51B78A59, 0x051D8181, 0x5352CAC9, 0x57D1312B, 0x2726ED57, 0x2E6BC515, -0x70736281, 0x5938B619, 0x0D4B6ACB, 0x44AB5E2B, 0x0029A485, 0x002CE54F, 0x075B0591, 0x3EACFDA9, -0x0AC03411, 0x53B00F73, 0x2066992D, 0x76E72223, 0x55F62A8D, 0x3FF92EE1, 0x17EE0EB3, 0x5E470AF1, -0x7193EB7F, 0x37A2CCD3, 0x7B44F7AF, 0x0FED8B3F, 0x4CC05805, 0x7352BF79, 0x3B61F755, 0x523CF9A3, -0x1AAFD219, 0x76035415, 0x5BE84287, 0x6D598909, 0x456537E9, 0x407EA83F, 0x23F6FFD5, 0x60256F39, -0x5D8EE59F, 0x35265CEB, 0x1D4AD4EF, 0x676E2E0F, 0x2D47932D, 0x776BB33B, 0x6DE1902B, 0x2C3F8741, -0x5B2DE8EF, 0x686DDB3B, 0x1D7C61C7, 0x1B061633, 0x3229EA51, 0x7FCB0E63, 0x5F22F4C9, 0x517A7199, -0x2A8D7973, 0x10DCD257, 0x41D59B27, 0x2C61CA67, 0x2020174F, 0x71653B01, 0x2FE464DD, 0x3E7ED6C7, -0x164D2A71, 0x5D4F3141, 0x5F7BABA7, 0x50E1C011, 0x140F5D77, 0x34E80809, 0x04AAC6B3, 0x29C42BAB, -0x08F9B6F7, 0x461E62FD, 0x45C2660B, 0x08BF25A7, 0x5494EA7B, 0x0225EBB7, 0x3C5A47CF, 0x2701C333, -0x457ED05B, 0x48CDDE55, 0x14083099, 0x7C69BDAB, 0x7BF163C9, 0x41EE1DAB, 0x258B1307, 0x0FFAD43B, -0x6601D767, 0x214DBEC7, 0x2852CCF5, 0x0009B471, 0x190AC89D, 0x5BDFB907, 0x15D4E331, 0x15D22375, -0x13F388D5, 0x12ACEDA5, 0x3835EA5D, 0x2587CA35, 0x06756643, 0x487C6F55, 0x65C295EB, 0x1029F2E1, -0x10CEF39D, 0x14C2E415, 0x444825BB, 0x24BE0A2F, 0x1D2B7C01, 0x64AE3235, 0x5D2896E5, 0x61BBBD87, -0x4A49E86D, 0x12C277FF, 0x72C81289, 0x5CF42A3D, 0x332FF177, 0x0DAECD23, 0x6000ED1D, 0x203CDDE1, -0x40C62CAD, 0x19B9A855, 0x782020C3, 0x6127D5BB, 0x719889A7, 0x40E4FCCF, 0x2A3C8FF9, 0x07411C7F, -0x3113306B, 0x4D7CA03F, 0x76119841, 0x54CEFBDF, 0x11548AB9, 0x4B0748EB, 0x569966B1, 0x45BC721B, -0x3D5A376B, 0x0D8923E9, 0x6D95514D, 0x0F39A367, 0x2FDAD92F, 0x721F972F, 0x42D0E21D, 0x5C5952DB, -0x7394D007, 0x02692C55, 0x7F92772F, 0x025F8025, 0x34347113, 0x560EA689, 0x0DCC21DF, 0x09ECC7F5, -0x091F3993, 0x0E0B52AB, 0x497CAA55, 0x0A040A49, 0x6D8F0CC5, 0x54F41609, 0x6E0CB8DF, 0x3DCB64C3, -0x16C365CD, 0x6D6B9FB5, 0x02B9382B, 0x6A5BFAF1, 0x1669D75F, 0x13CFD4FD, 0x0FDF316F, 0x21F3C463, -0x6FC58ABF, 0x04E45BE7, 0x1911225B, 0x28CD1355, 0x222084E9, 0x672AD54B, 0x476FC267, 0x6864E16D, -0x20AEF4FB, 0x603C5FB9, 0x55090595, 0x1113B705, 0x24E38493, 0x5291AF97, 0x5F5446D9, 0x13A6F639, -0x3D501313, 0x37E02017, 0x236B0ED3, 0x60F246BF, 0x01E02501, 0x2D2F66BD, 0x6BF23609, 0x16729BAF -}; + 0x02D4AF27, 0x1865DFC7, 0x47C62B43, 0x35B4889B, 0x210459A1, 0x3CC51CC7, 0x02ADD945, 0x0607C4D7, 0x558E6035, 0x0554224F, + 0x5A281657, 0x1C458C7F, 0x7F8BE723, 0x20B9BA99, 0x7218AA35, 0x64B10C2B, 0x548E8983, 0x5951218F, 0x7AADC871, 0x695FA5B1, + 0x40D40FCB, 0x20E03CC9, 0x55E9920F, 0x554CE08B, 0x7E78B1D7, 0x7D965DF9, 0x36A520A1, 0x1B0C6C11, 0x33385667, 0x2B0A7B9B, + 0x0F35AE23, 0x0BD608FB, 0x2284ADA3, 0x6E6C0687, 0x129B3EED, 0x7E86289D, 0x1143C24B, 0x1B6C7711, 0x1D87BB41, 0x4C7E635D, + 0x67577999, 0x0A0113C5, 0x6CF085B5, 0x14A4D0FB, 0x4E93E3A7, 0x5C87672B, 0x67F3CA17, 0x5F944339, 0x4C16DFD7, 0x5310C0E3, + 0x2FAD1447, 0x4AFB3187, 0x08468B7F, 0x49E56C51, 0x6280012F, 0x097D1A85, 0x34CC9403, 0x71028BD7, 0x6DEDC7E9, 0x64093291, + 0x6D78BB0B, 0x7A03B465, 0x2E044A43, 0x1AE58515, 0x23E495CD, 0x46102A83, 0x51B78A59, 0x051D8181, 0x5352CAC9, 0x57D1312B, + 0x2726ED57, 0x2E6BC515, 0x70736281, 0x5938B619, 0x0D4B6ACB, 0x44AB5E2B, 0x0029A485, 0x002CE54F, 0x075B0591, 0x3EACFDA9, + 0x0AC03411, 0x53B00F73, 0x2066992D, 0x76E72223, 0x55F62A8D, 0x3FF92EE1, 0x17EE0EB3, 0x5E470AF1, 0x7193EB7F, 0x37A2CCD3, + 0x7B44F7AF, 0x0FED8B3F, 0x4CC05805, 0x7352BF79, 0x3B61F755, 0x523CF9A3, 0x1AAFD219, 0x76035415, 0x5BE84287, 0x6D598909, + 0x456537E9, 0x407EA83F, 0x23F6FFD5, 0x60256F39, 0x5D8EE59F, 0x35265CEB, 0x1D4AD4EF, 0x676E2E0F, 0x2D47932D, 0x776BB33B, + 0x6DE1902B, 0x2C3F8741, 0x5B2DE8EF, 0x686DDB3B, 0x1D7C61C7, 0x1B061633, 0x3229EA51, 0x7FCB0E63, 0x5F22F4C9, 0x517A7199, + 0x2A8D7973, 0x10DCD257, 0x41D59B27, 0x2C61CA67, 0x2020174F, 0x71653B01, 0x2FE464DD, 0x3E7ED6C7, 0x164D2A71, 0x5D4F3141, + 0x5F7BABA7, 0x50E1C011, 0x140F5D77, 0x34E80809, 0x04AAC6B3, 0x29C42BAB, 0x08F9B6F7, 0x461E62FD, 0x45C2660B, 0x08BF25A7, + 0x5494EA7B, 0x0225EBB7, 0x3C5A47CF, 0x2701C333, 0x457ED05B, 0x48CDDE55, 0x14083099, 0x7C69BDAB, 0x7BF163C9, 0x41EE1DAB, + 0x258B1307, 0x0FFAD43B, 0x6601D767, 0x214DBEC7, 0x2852CCF5, 0x0009B471, 0x190AC89D, 0x5BDFB907, 0x15D4E331, 0x15D22375, + 0x13F388D5, 0x12ACEDA5, 0x3835EA5D, 0x2587CA35, 0x06756643, 0x487C6F55, 0x65C295EB, 0x1029F2E1, 0x10CEF39D, 0x14C2E415, + 0x444825BB, 0x24BE0A2F, 0x1D2B7C01, 0x64AE3235, 0x5D2896E5, 0x61BBBD87, 0x4A49E86D, 0x12C277FF, 0x72C81289, 0x5CF42A3D, + 0x332FF177, 0x0DAECD23, 0x6000ED1D, 0x203CDDE1, 0x40C62CAD, 0x19B9A855, 0x782020C3, 0x6127D5BB, 0x719889A7, 0x40E4FCCF, + 0x2A3C8FF9, 0x07411C7F, 0x3113306B, 0x4D7CA03F, 0x76119841, 0x54CEFBDF, 0x11548AB9, 0x4B0748EB, 0x569966B1, 0x45BC721B, + 0x3D5A376B, 0x0D8923E9, 0x6D95514D, 0x0F39A367, 0x2FDAD92F, 0x721F972F, 0x42D0E21D, 0x5C5952DB, 0x7394D007, 0x02692C55, + 0x7F92772F, 0x025F8025, 0x34347113, 0x560EA689, 0x0DCC21DF, 0x09ECC7F5, 0x091F3993, 0x0E0B52AB, 0x497CAA55, 0x0A040A49, + 0x6D8F0CC5, 0x54F41609, 0x6E0CB8DF, 0x3DCB64C3, 0x16C365CD, 0x6D6B9FB5, 0x02B9382B, 0x6A5BFAF1, 0x1669D75F, 0x13CFD4FD, + 0x0FDF316F, 0x21F3C463, 0x6FC58ABF, 0x04E45BE7, 0x1911225B, 0x28CD1355, 0x222084E9, 0x672AD54B, 0x476FC267, 0x6864E16D, + 0x20AEF4FB, 0x603C5FB9, 0x55090595, 0x1113B705, 0x24E38493, 0x5291AF97, 0x5F5446D9, 0x13A6F639, 0x3D501313, 0x37E02017, + 0x236B0ED3, 0x60F246BF, 0x01E02501, 0x2D2F66BD, 0x6BF23609, 0x16729BAF}; // binary search over intervals static int -i_find(const Intervals *i, int x) { +i_find(const Intervals *i, int x) +{ ink_assert(i->n); int l = 0, h = i->n; - Lrecurse: +Lrecurse: if (h <= l + 2) { if (h <= l) return -(l + 1); @@ -94,7 +85,8 @@ i_find(const Intervals *i, int x) { } bool -Intervals::in(int x) const { +Intervals::in(int x) const +{ if (!n) return false; if (i_find(this, x) > 0) @@ -104,7 +96,8 @@ Intervals::in(int x) const { // insert into interval with merge void -Intervals::insert(int x) { +Intervals::insert(int x) +{ if (!n) { add(x); add(x); @@ -142,16 +135,16 @@ Intervals::insert(int x) { goto Lmerge; } } - Lmore: +Lmore: fill(n + 2); if (n - 2 - l > 0) memmove(v + l + 2, v + l, sizeof(int) * (n - 2 - l)); v[l] = x; - v[l+1] = x; + v[l + 1] = x; return; - Lmerge: +Lmerge: if (l) { - if (v[l] - v[l-1] < 2) { + if (v[l] - v[l - 1] < 2) { l -= 2; goto Ldomerge; } @@ -161,14 +154,15 @@ Intervals::insert(int x) { goto Ldomerge; } return; - Ldomerge: +Ldomerge: memmove(v + l + 1, v + l + 3, sizeof(int) * (n - 3 - l)); n -= 2; goto Lmerge; } void -UnionFind::size(int s) { +UnionFind::size(int s) +{ size_t nn = n; fill(s); for (size_t i = nn; i < n; i++) @@ -176,9 +170,11 @@ UnionFind::size(int s) { } int -UnionFind::find(int n) { +UnionFind::find(int n) +{ int i, t; - for (i = n; v[i] >= 0; i = v[i]); + for (i = n; v[i] >= 0; i = v[i]) + ; while (v[n] >= 0) { t = n; n = v[n]; @@ -188,7 +184,8 @@ UnionFind::find(int n) { } void -UnionFind::unify(int n, int m) { +UnionFind::unify(int n, int m) +{ n = find(n); m = find(m); if (n != m) {
