Browse Source

cleanup duk_substring() to take an index instead of working with stack top only; minor duk_api.h cleanup

pull/1/head
Sami Vaarala 11 years ago
parent
commit
f11b8e74de
  1. 19
      src/duk_api.h
  2. 7
      src/duk_api_string.c
  3. 4
      src/duk_builtin_json.c
  4. 8
      src/duk_builtin_string.c
  5. 6
      src/duk_hobject_props.c

19
src/duk_api.h

@ -92,21 +92,6 @@ struct duk_memory_functions {
#define DUK_TYPE_MASK_BUFFER (1 << DUK_TYPE_BUFFER)
#define DUK_TYPE_MASK_POINTER (1 << DUK_TYPE_POINTER)
/* FIXME: these are now used by duk_get_multiple() and duk_push_multiple(),
* but the type chararacters are not very logical and don't cover all the
* current types.
*/
#define DUK_TYPECHAR_UNDEFINED 'u'
#define DUK_TYPECHAR_NULL 'n'
#define DUK_TYPECHAR_BOOLEAN 'b'
#define DUK_TYPECHAR_NUMBER 'd'
#define DUK_TYPECHAR_INTEGER 'i'
#define DUK_TYPECHAR_STRING 's'
#define DUK_TYPECHAR_LSTRING 'l'
/* FIXME: buffer */
#define DUK_TYPECHAR_POINTER 'p'
#define DUK_TYPECHAR_SKIP '-'
/* Coercion hints */
#define DUK_HINT_NONE 0 /* prefer number, unless coercion input is a Date, in which case prefer string (E5 Section 8.12.8) */
#define DUK_HINT_STRING 1 /* prefer string */
@ -270,7 +255,6 @@ const char *duk_push_lstring(duk_context *ctx, const char *str, size_t len);
void duk_push_pointer(duk_context *ctx, void *p);
const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...); /* may fail */
const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap); /* may fail */
void duk_push_multiple(duk_context *ctx, const char *types, ...); /* push multiple values, see duk_get_multiple */
void duk_push_this(duk_context *ctx); /* push the current 'this' binding */
void duk_push_current_function(duk_context *ctx); /* push the currently running (C) function */
@ -347,7 +331,6 @@ const char *duk_get_string(duk_context *ctx, int index); /* defa
const char *duk_get_lstring(duk_context *ctx, int index, size_t *out_len); /* default: NULL, out_len may be NULL */
void *duk_get_buffer(duk_context *ctx, int index, size_t *out_size); /* default: NULL, out_size may be NULL */
void *duk_get_pointer(duk_context *ctx, int index); /* default: NULL */
void duk_get_multiple(duk_context *ctx, int start_index, const char *types, ...);
duk_c_function duk_get_c_function(duk_context *ctx, int index);
duk_context *duk_get_context(duk_context *ctx, int index);
size_t duk_get_length(duk_context *ctx, int index); /* string: char length,
@ -468,7 +451,7 @@ void duk_concat(duk_context *ctx, unsigned int count); /* [val1 ...
void duk_join(duk_context *ctx, unsigned int count); /* [sep val1 ... valN] -> [res], coerced and joined */
void duk_decode_string(duk_context *ctx, int index, duk_decode_char_function callback, void *udata);
void duk_map_string(duk_context *ctx, int index, duk_map_char_function callback, void *udata);
void duk_substring(duk_context *ctx, size_t start_offset, size_t end_offset);
void duk_substring(duk_context *ctx, int index, size_t start_offset, size_t end_offset);
void duk_trim(duk_context *ctx, int index); /* trim using StrWhiteSpaceChar: WhiteSpace + LineTerminator,
* matches String.prototype.trim(), global object parseInt()
* and parseFloat().

7
src/duk_api_string.c

@ -99,7 +99,7 @@ void duk_map_string(duk_context *ctx, int index, duk_map_char_function callback,
DUK_ERROR((duk_hthread *) ctx, DUK_ERR_UNIMPLEMENTED_ERROR, "FIXME");
}
void duk_substring(duk_context *ctx, size_t start_offset, size_t end_offset) {
void duk_substring(duk_context *ctx, int index, size_t start_offset, size_t end_offset) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hstring *h;
duk_hstring *res;
@ -108,7 +108,8 @@ void duk_substring(duk_context *ctx, size_t start_offset, size_t end_offset) {
DUK_ASSERT(ctx != NULL);
h = duk_require_hstring(ctx, -1);
index = duk_require_normalize_index(ctx, index);
h = duk_require_hstring(ctx, index);
DUK_ASSERT(h != NULL);
if (end_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
@ -131,7 +132,7 @@ void duk_substring(duk_context *ctx, size_t start_offset, size_t end_offset) {
end_byte_offset - start_byte_offset);
duk_push_hstring(ctx, res);
duk_remove(ctx, -2);
duk_replace(ctx, index);
}
/* FIXME: this is quite clunky. Add Unicode helpers to scan backwards and

4
src/duk_builtin_json.c

@ -1578,9 +1578,9 @@ void duk_builtin_json_stringify_helper(duk_context *ctx,
js_ctx->h_gap = duk_get_hstring(ctx, -1);
DUK_ASSERT(js_ctx->h_gap != NULL);
} else if (duk_is_string(ctx, idx_space)) {
/* FIXME: substring API requires a dup */
/* FIXME: substring in-place at idx_place? */
duk_dup(ctx, idx_space);
duk_substring(ctx, 0, 10); /* clamp to 10 chars */
duk_substring(ctx, -1, 0, 10); /* clamp to 10 chars */
js_ctx->h_gap = duk_get_hstring(ctx, -1);
DUK_ASSERT(js_ctx->h_gap != NULL);
} else {

8
src/duk_builtin_string.c

@ -118,7 +118,7 @@ int duk_builtin_string_prototype_char_at(duk_context *ctx) {
duk_push_this_coercible_to_string(ctx);
pos = duk_to_int(ctx, 0);
duk_substring(ctx, pos, pos + 1);
duk_substring(ctx, -1, pos, pos + 1);
return 1;
}
@ -205,7 +205,7 @@ int duk_builtin_string_prototype_substring(duk_context *ctx) {
DUK_ASSERT(end_pos >= start_pos);
duk_substring(ctx, (size_t) start_pos, (size_t) end_pos);
duk_substring(ctx, -1, (size_t) start_pos, (size_t) end_pos);
return 1;
}
@ -255,7 +255,7 @@ int duk_builtin_string_prototype_substr(duk_context *ctx) {
DUK_ASSERT(end_pos >= 0 && end_pos <= len);
DUK_ASSERT(end_pos >= start_pos);
duk_substring(ctx, (size_t) start_pos, (size_t) end_pos);
duk_substring(ctx, -1, (size_t) start_pos, (size_t) end_pos);
return 1;
}
#endif /* DUK_USE_SECTION_B */
@ -298,7 +298,7 @@ int duk_builtin_string_prototype_slice(duk_context *ctx) {
DUK_ASSERT(end_pos >= start_pos);
duk_substring(ctx, (size_t) start_pos, (size_t) end_pos);
duk_substring(ctx, -1, (size_t) start_pos, (size_t) end_pos);
return 1;
}

6
src/duk_hobject_props.c

@ -1359,7 +1359,7 @@ static int get_own_property_desc_raw(duk_hthread *thr, duk_hobject *obj, duk_hst
DUK_DDDPRINT("-> found, array index inside string");
if (push_value) {
duk_push_hstring(ctx, h_val);
duk_substring(ctx, arr_idx, arr_idx + 1); /* [str] -> [substr] */
duk_substring(ctx, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
}
out_desc->flags = DUK_PROPDESC_FLAG_ENUMERABLE; /* E5 Section 15.5.5.2 */
@ -1693,7 +1693,7 @@ int duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
if ((double) idx == t && /* is whole and >= 0 */
idx < DUK_HSTRING_GET_CHARLEN(h)) { /* and inside string */
duk_push_hstring(ctx, h);
duk_substring(ctx, idx, idx + 1); /* [str] -> [substr] */
duk_substring(ctx, -1, idx, idx + 1); /* [str] -> [substr] */
DUK_DDDPRINT("-> %!T (base is a string, key is a whole number "
"inside string length -> return char)",
@ -1724,7 +1724,7 @@ int duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
duk_pop(ctx); /* [key] -> [] */
duk_push_hstring(ctx, h);
duk_substring(ctx, arr_idx, arr_idx + 1); /* [str] -> [substr] */
duk_substring(ctx, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
DUK_DDDPRINT("-> %!T (base is string, key is an index inside string length "
"after coercion -> return char)",

Loading…
Cancel
Save