{
size_t size;
time_t clock;
- u_char *start;
u_char buf[NJS_DATE_TIME_LEN];
struct tm tm;
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;
double time;
size_t size;
time_t clock;
- u_char *start;
u_char buf[NJS_DATE_TIME_LEN];
struct tm tm;
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;
double time;
size_t size;
time_t clock;
- u_char *start;
u_char buf[NJS_ISO_DATE_TIME_LEN];
struct tm tm;
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;
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;
fmt = "%1.e";
}
- size = snprintf(buf, sizeof(buf), fmt, num);
+ size = snprintf((char *) buf, sizeof(buf), fmt, num);
- p = njs_string_alloc(vm, string, size, size);
-
- 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;
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;
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);
}
}
+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)
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;
}
/* 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;
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;
}
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);
}
}
} 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;