Fossil SCM

Add the [string trimleft] and [string trimright] commands to TH1.

drh 2012-11-23 21:45 UTC ticket-enhancements
Commit b28ae72e846c1951536f37e773bec5ec541c83f3
1 file changed +11 -3
+11 -3
--- src/th_lang.c
+++ src/th_lang.c
@@ -819,10 +819,12 @@
819819
820820
/*
821821
** TH Syntax:
822822
**
823823
** string trim STRING
824
+** string trimleft STRING
825
+** string trimright STRING
824826
*/
825827
static int string_trim_command(
826828
Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl
827829
){
828830
int n;
@@ -831,12 +833,16 @@
831833
if( argc!=3 ){
832834
return Th_WrongNumArgs(interp, "string trim string");
833835
}
834836
z = argv[2];
835837
n = argl[2];
836
- while( n && th_isspace(z[0]) ){ z++; n--; }
837
- while( n && th_isspace(z[n-1]) ){ n--; }
838
+ if( argl[1]<5 || argv[1][4]=='l' ){
839
+ while( n && th_isspace(z[0]) ){ z++; n--; }
840
+ }
841
+ if( argl[1]<5 || argv[1][4]=='r' ){
842
+ while( n && th_isspace(z[n-1]) ){ n--; }
843
+ }
838844
Th_SetResult(interp, z, n);
839845
return TH_OK;
840846
}
841847
842848
/*
@@ -919,11 +925,13 @@
919925
{ "is", string_is_command },
920926
{ "last", string_last_command },
921927
{ "length", string_length_command },
922928
{ "range", string_range_command },
923929
{ "repeat", string_repeat_command },
924
- { "trim", string_trim_command },
930
+ { "trim", string_trim_command },
931
+ { "trimleft", string_trim_command },
932
+ { "trimright", string_trim_command },
925933
{ 0, 0 }
926934
};
927935
return Th_CallSubCommand(interp, ctx, argc, argv, argl, aSub);
928936
}
929937
930938
--- src/th_lang.c
+++ src/th_lang.c
@@ -819,10 +819,12 @@
819
820 /*
821 ** TH Syntax:
822 **
823 ** string trim STRING
 
 
824 */
825 static int string_trim_command(
826 Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl
827 ){
828 int n;
@@ -831,12 +833,16 @@
831 if( argc!=3 ){
832 return Th_WrongNumArgs(interp, "string trim string");
833 }
834 z = argv[2];
835 n = argl[2];
836 while( n && th_isspace(z[0]) ){ z++; n--; }
837 while( n && th_isspace(z[n-1]) ){ n--; }
 
 
 
 
838 Th_SetResult(interp, z, n);
839 return TH_OK;
840 }
841
842 /*
@@ -919,11 +925,13 @@
919 { "is", string_is_command },
920 { "last", string_last_command },
921 { "length", string_length_command },
922 { "range", string_range_command },
923 { "repeat", string_repeat_command },
924 { "trim", string_trim_command },
 
 
925 { 0, 0 }
926 };
927 return Th_CallSubCommand(interp, ctx, argc, argv, argl, aSub);
928 }
929
930
--- src/th_lang.c
+++ src/th_lang.c
@@ -819,10 +819,12 @@
819
820 /*
821 ** TH Syntax:
822 **
823 ** string trim STRING
824 ** string trimleft STRING
825 ** string trimright STRING
826 */
827 static int string_trim_command(
828 Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl
829 ){
830 int n;
@@ -831,12 +833,16 @@
833 if( argc!=3 ){
834 return Th_WrongNumArgs(interp, "string trim string");
835 }
836 z = argv[2];
837 n = argl[2];
838 if( argl[1]<5 || argv[1][4]=='l' ){
839 while( n && th_isspace(z[0]) ){ z++; n--; }
840 }
841 if( argl[1]<5 || argv[1][4]=='r' ){
842 while( n && th_isspace(z[n-1]) ){ n--; }
843 }
844 Th_SetResult(interp, z, n);
845 return TH_OK;
846 }
847
848 /*
@@ -919,11 +925,13 @@
925 { "is", string_is_command },
926 { "last", string_last_command },
927 { "length", string_length_command },
928 { "range", string_range_command },
929 { "repeat", string_repeat_command },
930 { "trim", string_trim_command },
931 { "trimleft", string_trim_command },
932 { "trimright", string_trim_command },
933 { 0, 0 }
934 };
935 return Th_CallSubCommand(interp, ctx, argc, argv, argl, aSub);
936 }
937
938

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button