[njs] njs_string_new().
Igor Sysoev
igor at sysoev.ru
Wed Jun 1 12:50:24 UTC 2016
details: http://hg.nginx.org/njs/rev/f720a6378a2f
branches:
changeset: 103:f720a6378a2f
user: Igor Sysoev <igor at sysoev.ru>
date: Tue Apr 19 16:07:33 2016 +0300
description:
njs_string_new().
diffstat:
njs/njs_date.c | 30 +++---------------------------
njs/njs_number.c | 25 +++++--------------------
njs/njs_string.c | 51 +++++++++++++++++++++++++--------------------------
njs/njs_string.h | 2 ++
4 files changed, 35 insertions(+), 73 deletions(-)
diffs (237 lines):
diff -r 23ff7c369101 -r f720a6378a2f njs/njs_date.c
--- a/njs/njs_date.c Tue Apr 19 17:26:25 2016 +0300
+++ b/njs/njs_date.c Tue Apr 19 16:07:33 2016 +0300
@@ -831,7 +831,6 @@ njs_date_string(njs_vm_t *vm, const char
{
size_t size;
time_t clock;
- u_char *start;
u_char buf[NJS_DATE_TIME_LEN];
struct tm tm;
@@ -841,14 +840,7 @@ njs_date_string(njs_vm_t *vm, const char
size = strftime((char *) buf, NJS_DATE_TIME_LEN, fmt, &tm);
- start = njs_string_alloc(vm, &vm->retval, size, size);
-
- if (nxt_fast_path(start != NULL)) {
- memcpy(start, buf, size);
- return NXT_OK;
- }
-
- return NXT_ERROR;
+ return njs_string_new(vm, &vm->retval, buf, size, size);
}
vm->retval = njs_string_invalid_date;
@@ -864,7 +856,6 @@ njs_date_prototype_to_utc_string(njs_vm_
double time;
size_t size;
time_t clock;
- u_char *start;
u_char buf[NJS_DATE_TIME_LEN];
struct tm tm;
@@ -886,14 +877,7 @@ njs_date_prototype_to_utc_string(njs_vm_
tm.tm_mday, tm.tm_year + 1900,
tm.tm_hour, tm.tm_min, tm.tm_sec);
- start = njs_string_alloc(vm, &vm->retval, size, size);
-
- if (nxt_fast_path(start != NULL)) {
- memcpy(start, buf, size);
- return NXT_OK;
- }
-
- return NXT_ERROR;
+ return njs_string_new(vm, &vm->retval, buf, size, size);
}
vm->retval = njs_string_invalid_date;
@@ -910,7 +894,6 @@ njs_date_prototype_to_iso_string(njs_vm_
double time;
size_t size;
time_t clock;
- u_char *start;
u_char buf[NJS_ISO_DATE_TIME_LEN];
struct tm tm;
@@ -930,14 +913,7 @@ njs_date_prototype_to_iso_string(njs_vm_
tm.tm_hour, tm.tm_min, tm.tm_sec,
(int) ((int64_t) time % 1000));
- start = njs_string_alloc(vm, &vm->retval, size, size);
-
- if (nxt_fast_path(start != NULL)) {
- memcpy(start, buf, size);
- return NXT_OK;
- }
-
- return NXT_ERROR;
+ return njs_string_new(vm, &vm->retval, buf, size, size);
}
vm->exception = &njs_exception_range_error;
diff -r 23ff7c369101 -r f720a6378a2f njs/njs_number.c
--- a/njs/njs_number.c Tue Apr 19 17:26:25 2016 +0300
+++ b/njs/njs_number.c Tue Apr 19 16:07:33 2016 +0300
@@ -167,12 +167,11 @@ njs_ret_t
njs_number_to_string(njs_vm_t *vm, njs_value_t *string,
const njs_value_t *number)
{
- u_char *p;
double n, num;
size_t size;
const char *fmt;
const njs_value_t *value;
- char buf[128];
+ u_char buf[128];
num = number->data.u.number;
@@ -207,16 +206,9 @@ njs_number_to_string(njs_vm_t *vm, njs_v
fmt = "%1.e";
}
- size = snprintf(buf, sizeof(buf), fmt, num);
-
- p = njs_string_alloc(vm, string, size, size);
+ size = snprintf((char *) buf, sizeof(buf), fmt, num);
- if (nxt_fast_path(p != NULL)) {
- memcpy(p, buf, size);
- return NXT_OK;
- }
-
- return NXT_ERROR;
+ return njs_string_new(vm, string, buf, size, size);
}
*string = *value;
@@ -363,7 +355,7 @@ static njs_ret_t
njs_number_to_string_radix(njs_vm_t *vm, njs_value_t *string,
const njs_value_t *number, uint32_t radix)
{
- u_char *p, *f, *start, *end;
+ u_char *p, *f, *end;
double n, next;
size_t size;
uint8_t reminder;
@@ -410,14 +402,7 @@ njs_number_to_string_radix(njs_vm_t *vm,
size = f - p;
- start = njs_string_alloc(vm, string, size, size);
-
- if (nxt_fast_path(start != NULL)) {
- memcpy(start, p, size);
- return NXT_OK;
- }
-
- return NXT_ERROR;
+ return njs_string_new(vm, string, p, size, size);
}
diff -r 23ff7c369101 -r f720a6378a2f njs/njs_string.c
--- a/njs/njs_string.c Tue Apr 19 17:26:25 2016 +0300
+++ b/njs/njs_string.c Tue Apr 19 16:07:33 2016 +0300
@@ -92,6 +92,28 @@ njs_string_create(njs_vm_t *vm, njs_valu
}
+nxt_noinline njs_ret_t
+njs_string_new(njs_vm_t *vm, njs_value_t *value, const u_char *start,
+ uint32_t size, uint32_t length)
+{
+ u_char *p;
+
+ p = njs_string_alloc(vm, value, size, length);
+
+ if (nxt_fast_path(p != NULL)) {
+ memcpy(p, start, size);
+
+ if (size != length && length >= NJS_STRING_MAP_OFFSET) {
+ njs_string_offset_map_init(p, size);
+ }
+
+ return NXT_OK;
+ }
+
+ return NXT_ERROR;
+}
+
+
nxt_noinline u_char *
njs_string_alloc(njs_vm_t *vm, njs_value_t *value, uint32_t size,
uint32_t length)
@@ -571,7 +593,6 @@ static njs_ret_t
njs_string_prototype_from_utf8(njs_vm_t *vm, njs_value_t *args,
nxt_uint_t nargs, njs_index_t unused)
{
- u_char *p;
ssize_t length;
njs_slice_prop_t slice;
njs_string_prop_t string;
@@ -596,17 +617,8 @@ njs_string_prototype_from_utf8(njs_vm_t
}
/* Long UTF-8 string. */
-
- p = njs_string_alloc(vm, &vm->retval, slice.length, length);
-
- if (nxt_fast_path(p != NULL)) {
- memcpy(p, string.start, slice.length);
- njs_string_offset_map_init(p, slice.length);
-
- return NXT_OK;
- }
-
- return NXT_ERROR;
+ return njs_string_new(vm, &vm->retval, string.start, slice.length,
+ length);
}
vm->retval = njs_value_null;
@@ -949,7 +961,6 @@ nxt_noinline njs_ret_t
njs_string_slice(njs_vm_t *vm, njs_value_t *dst,
const njs_string_prop_t *string, njs_slice_prop_t *slice)
{
- u_char *s;
size_t size, n, length;
ssize_t excess;
const u_char *p, *start, *end;
@@ -994,19 +1005,7 @@ njs_string_slice(njs_vm_t *vm, njs_value
}
if (nxt_fast_path(size != 0)) {
- s = njs_string_alloc(vm, &vm->retval, size, length);
-
- if (nxt_slow_path(s == NULL)) {
- return NXT_ERROR;
- }
-
- memcpy(s, start, size);
-
- if (length >= NJS_STRING_MAP_OFFSET && size != length) {
- njs_string_offset_map_init(s, size);
- }
-
- return NXT_OK;
+ return njs_string_new(vm, &vm->retval, start, size, length);
}
}
diff -r 23ff7c369101 -r f720a6378a2f njs/njs_string.h
--- a/njs/njs_string.h Tue Apr 19 17:26:25 2016 +0300
+++ b/njs/njs_string.h Tue Apr 19 16:07:33 2016 +0300
@@ -80,6 +80,8 @@ typedef struct {
} njs_slice_prop_t;
+njs_ret_t njs_string_new(njs_vm_t *vm, njs_value_t *value, const u_char *start,
+ uint32_t size, uint32_t length);
u_char *njs_string_alloc(njs_vm_t *vm, njs_value_t *value, uint32_t size,
uint32_t length)
NXT_MALLOC_LIKE;
More information about the nginx-devel
mailing list