diff --git a/include/bx/url.h b/include/bx/url.h index 9964e55..11172a4 100644 --- a/include/bx/url.h +++ b/include/bx/url.h @@ -45,7 +45,7 @@ namespace bx }; /// - void urlEncode(const char* _str, char* _buf, uint32_t _bufSize); + void urlEncode(char* _out, uint32_t _max, const StringView& _str); } // namespace bx diff --git a/src/dtoa.cpp b/src/dtoa.cpp index 6aa8d0f..5b1858c 100644 --- a/src/dtoa.cpp +++ b/src/dtoa.cpp @@ -1102,7 +1102,7 @@ namespace bx int32_t result = 0; - for (ch = *str++; isNumeric(ch) && str != term+1; ch = *str++) + for (ch = *str++; isNumeric(ch) && str <= term; ch = *str++) { result = 10*result - (ch - '0'); } diff --git a/src/url.cpp b/src/url.cpp index 7f3e3ab..3f01333 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -122,14 +122,17 @@ namespace bx } // https://secure.wikimedia.org/wikipedia/en/wiki/URL_encoding - void urlEncode(const char* _str, char* _buf, uint32_t _bufSize) + void urlEncode(char* _out, uint32_t _max, const StringView& _str) { - _bufSize--; // need space for zero terminator + _max--; // need space for zero terminator + + const char* str = _str.getPtr(); + const char* term = _str.getTerm(); uint32_t ii = 0; - for (char ch = *_str++ - ; '\0' != ch && ii < _bufSize - ; ch = *_str++ + for (char ch = *str++ + ; str <= term && ii < _max + ; ch = *str++ ) { if (isAlphaNum(ch) @@ -138,17 +141,17 @@ namespace bx || ch == '.' || ch == '~') { - _buf[ii++] = ch; + _out[ii++] = ch; } - else if (ii+3 < _bufSize) + else if (ii+3 < _max) { - _buf[ii++] = '%'; - _buf[ii++] = toHex(ch>>4); - _buf[ii++] = toHex(ch); + _out[ii++] = '%'; + _out[ii++] = toHex(ch>>4); + _out[ii++] = toHex(ch); } } - _buf[ii] = '\0'; + _out[ii] = '\0'; } } // namespace bx