diff options
author | mongo <mongo@iomega> | 2016-10-20 16:49:50 -0300 |
---|---|---|
committer | mongo <mongo@iomega> | 2016-10-20 16:49:50 -0300 |
commit | 5abb22b995afafb0e651436a96ee15f9b75aec5e (patch) | |
tree | 7fbd1cde3bf709b1365b2423f8188a20e2ff088e | |
parent | d4476b05d77308ce216281ecd17f037061fa0c27 (diff) |
new @replace string function
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | src/doc | 8 | ||||
-rw-r--r-- | src/gram.y | 3 | ||||
-rw-r--r-- | src/interp.c | 8 | ||||
-rw-r--r-- | src/sc.h | 1 |
5 files changed, 22 insertions, 0 deletions
@@ -33,6 +33,8 @@ v0.4.0 * Added UNDO and LOCK check to fcopy special command. Added fcopy to gram.y * sum special command renamed to fsum * csv import now use isnumeric function +* change in '#' command +* new @replace string function v0.3.0 * FIX when importing large CSV files @@ -872,6 +872,8 @@ Commands for handling cell content: displays the string ``the lazy dog'' in the cell if the value of A0's string is ``the la''. &Built-in String Functions& + String functions can be entered typing \" + @substr(se,e1,e2) Extract and return from string expression se the substring indexed by character number e1 through character number e2 (defaults to the size of se if beyond the end of it). If e1 is less than 1 or @@ -889,6 +891,12 @@ Commands for handling cell content: will convert the first letter of words in a string into upper case and other letters to lower case (the latter if all letters of the string are upper case). + @replace(se, eold, enew) + Replace eold occurences in se string with enew string. + For example, having in A1 the string "Extension" and entering in A2 the following: + @replace(A1,"n","Z") + will result "Extezsioz". + @ext(se,e) Call an external function (program or script). The purpose is to allow arbitrary functions on val‐ ues, e.g. table lookups and interpolations. String expression se is a command or command line to @@ -248,6 +248,7 @@ token S_YANKCOL %token K_DTS %token K_TTS %token K_FMT +%token K_REPLACE %token K_SUBSTR %token K_UPPER %token K_LOWER @@ -758,6 +759,8 @@ term: var { $$ = new_var(O_VAR, $1); } | '@' K_EXT '(' e ',' e ')' { $$ = new(EXT, $4, $6); } | '@' K_NVAL '(' e ',' e ')' { $$ = new(NVAL, $4, $6); } | '@' K_SVAL '(' e ',' e ')' { $$ = new(SVAL, $4, $6); } + | '@' K_REPLACE '(' e ',' e ',' e ')' + { $$ = new(REPLACE, $4, new(',', $6, $8)); } | '@' K_SUBSTR '(' e ',' e ',' e ')' { $$ = new(SUBSTR, $4, new(',', $6, $8)); } | '(' e ')' { $$ = $2; } diff --git a/src/interp.c b/src/interp.c index ee6fa91..6730ff5 100644 --- a/src/interp.c +++ b/src/interp.c @@ -38,6 +38,7 @@ #include "xmalloc.h" // for scxfree #include "lex.h" // for atocol #include "interp.h" +#include "utils/string.h" #include <unistd.h> #ifdef UNDO @@ -1147,6 +1148,9 @@ char * dosval(char * colstr, double rowdoub) { return (strcpy(scxmalloc( (size_t) (strlen(llabel) + 1)), llabel)); } +char * doreplace(char * source, char * old, char * new) { + return str_replace(source, old, new); +} /* * Substring: Note that v1 and v2 are one-based to users, but zero-based @@ -1311,6 +1315,9 @@ char * seval(register struct ent * ent, register struct enode * se) { } case EXT: return (doext(se)); case SVAL: return (dosval(seval(ent, se->e.o.left), eval(NULL, se->e.o.right))); + case REPLACE: return (doreplace(seval(ent, se->e.o.left), + seval(NULL, se->e.o.right->e.o.left), + seval(NULL, se->e.o.right->e.o.right))); case SUBSTR: return (dosubstr(seval(ent, se->e.o.left), (int) eval(NULL, se->e.o.right->e.o.left) - 1, (int) eval(NULL, se->e.o.right->e.o.right) - 1)); @@ -2465,6 +2472,7 @@ void decompile(register struct enode *e, int priority) { case SVAL: two_arg("@sval(", e); break; case EXT: two_arg("@ext(", e); break; case SUBSTR: three_arg("@substr(", e); break; + case REPLACE: three_arg("@replace(", e); break; case STINDEX: index_arg("@stindex", e); break; case INDEX: index_arg("@index", e); break; case LOOKUP: index_arg("@lookup", e); break; @@ -233,6 +233,7 @@ struct go_save { #define ASCII (OP_BASE + 78) #define CHR (OP_BASE + 79) #define SET8BIT (OP_BASE + 80) +#define REPLACE (OP_BASE + 81) /* flag values */ #define is_valid 0001 |