Switching code to use StringView.

This commit is contained in:
Branimir Karadžić
2017-09-30 21:48:49 -07:00
parent 3883f52cb8
commit 82185277e4
3 changed files with 16 additions and 13 deletions

View File

@@ -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

View File

@@ -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');
}

View File

@@ -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