diff options
| author | Richard Maw <mawr8@cs.man.ac.uk> | 2010-03-10 20:28:09 +0000 | 
|---|---|---|
| committer | Charlie Brej <cbrej@cs.man.ac.uk> | 2010-03-10 20:28:09 +0000 | 
| commit | 9b3a1b672bbaea2ba93ec47af39713e26610dcb2 (patch) | |
| tree | ed5444584cd922a2601b5be15cf88fc611595faa | |
| parent | bb96d5ad128a2c9bdedf755a8186a1d8bc929063 (diff) | |
[script] Add SubString function to the script string library
Adds the SubString function which returns a string segment. The two paramiters
are the sub-string start and end indicies. Negative start and end values return
a NULL, as does start index being beyond the end index. Start being beyond the
end of the string returns an empty string.
| -rw-r--r-- | src/plugins/splash/script/script-lib-string.c | 40 | 
1 files changed, 40 insertions, 0 deletions
| diff --git a/src/plugins/splash/script/script-lib-string.c b/src/plugins/splash/script/script-lib-string.c index 833a3771..dbd63fe7 100644 --- a/src/plugins/splash/script/script-lib-string.c +++ b/src/plugins/splash/script/script-lib-string.c @@ -25,6 +25,7 @@  #include "script-execute.h"  #include "script-object.h"  #include "script-lib-string.h" +#include "ply-utils.h"  #include <assert.h>  #include <stdio.h>  #include <stdlib.h> @@ -62,6 +63,38 @@ static script_return_t script_lib_string_char_at (script_state_t *state,    return script_return_obj(script_obj_new_string (charstring));  } +static script_return_t script_lib_string_sub_string (script_state_t *state, +                                                     void           *user_data) +{ +  char *text = script_obj_as_string (state->this); +  int start = script_obj_hash_get_number (state->local, "start"); +  int end = script_obj_hash_get_number (state->local, "end"); +  int text_count; +  char* substring; +  script_obj_t *substring_obj; + +  if (!text || start < 0 || end < start) +    { +      free (text); +      return script_return_obj_null (); +    } + +  for (text_count = 0; text_count < start; text_count++) +    { +      if (text[text_count] == '\0') +        { +          free (text); +          return script_return_obj(script_obj_new_string ("")); +        } +    } + +  substring = strndup(&text[text_count], end - start); +  substring_obj = script_obj_new_string (substring); +  free (substring); +  free (text); +  return script_return_obj(substring_obj); +} +  script_lib_string_data_t *script_lib_string_setup (script_state_t *state)  {    script_lib_string_data_t *data = malloc (sizeof (script_lib_string_data_t)); @@ -73,6 +106,13 @@ script_lib_string_data_t *script_lib_string_setup (script_state_t *state)                                NULL,                                "index",                                NULL); +  script_add_native_function (string_hash, +                              "SubString", +                              script_lib_string_sub_string, +                              NULL, +                              "start", +                              "end", +                              NULL);    script_obj_unref (string_hash);    data->script_main_op = script_parse_string (script_lib_string_string, "script-lib-string.script");    script_return_t ret = script_execute (state, data->script_main_op); | 
