Skip to content

Commit

Permalink
String.bytesFrom() method.
Browse files Browse the repository at this point in the history
The method creates a byte string from an array containing octets:
    String.bytesFrom(array).

The method creates a byte string from an encoded string:
    String.bytesFrom(string, encoding)
where encoding are "hex", "base64", "base64url".

This closes #2 issue on Github.
  • Loading branch information
xeioex committed Jul 18, 2018
1 parent 8e20d5d commit 620ffc9
Show file tree
Hide file tree
Showing 5 changed files with 413 additions and 16 deletions.
21 changes: 5 additions & 16 deletions njs/njs_number.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,32 +215,21 @@ njs_number_bin_parse(const u_char **start, const u_char *end)
uint64_t
njs_number_hex_parse(const u_char **start, const u_char *end)
{
u_char c;
uint64_t num;
nxt_int_t n;
const u_char *p;

p = *start;

num = 0;

while (p < end) {
c = (u_char) (*p | 0x20);

/* Values less than '0' become >= 208. */
c = c - '0';

if (c > 9) {
/* Values less than 'a' become >= 159. */
c = c - ('a' - '0');

if (nxt_slow_path(c > 5)) {
break;
}

c += 10;
n = njs_char_to_hex(*p);
if (nxt_slow_path(n < 0)) {
break;
}

num = num * 16 + c;
num = num * 16 + n;
p++;
}

Expand Down
23 changes: 23 additions & 0 deletions njs/njs_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ njs_ret_t njs_number_parse_float(njs_vm_t *vm, njs_value_t *args,
nxt_noinline uint32_t njs_number_to_integer(double num);


nxt_inline nxt_int_t
njs_char_to_hex(u_char c)
{
c |= 0x20;

/* Values less than '0' become >= 208. */
c = c - '0';

if (c > 9) {
/* Values less than 'a' become >= 159. */
c = c - ('a' - '0');

if (nxt_slow_path(c > 5)) {
return -1;
}

c += 10;
}

return c;
}


extern const njs_object_init_t njs_number_constructor_init;
extern const njs_object_init_t njs_number_prototype_init;

Expand Down
Loading

0 comments on commit 620ffc9

Please sign in to comment.