Fossil SCM
Add the [string trimleft] and [string trimright] commands to TH1.
Commit
b28ae72e846c1951536f37e773bec5ec541c83f3
Parent
5139ad6e3604d4b…
1 file changed
+11
-3
+11
-3
| --- src/th_lang.c | ||
| +++ src/th_lang.c | ||
| @@ -819,10 +819,12 @@ | ||
| 819 | 819 | |
| 820 | 820 | /* |
| 821 | 821 | ** TH Syntax: |
| 822 | 822 | ** |
| 823 | 823 | ** string trim STRING |
| 824 | +** string trimleft STRING | |
| 825 | +** string trimright STRING | |
| 824 | 826 | */ |
| 825 | 827 | static int string_trim_command( |
| 826 | 828 | Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl |
| 827 | 829 | ){ |
| 828 | 830 | int n; |
| @@ -831,12 +833,16 @@ | ||
| 831 | 833 | if( argc!=3 ){ |
| 832 | 834 | return Th_WrongNumArgs(interp, "string trim string"); |
| 833 | 835 | } |
| 834 | 836 | z = argv[2]; |
| 835 | 837 | 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 | + } | |
| 838 | 844 | Th_SetResult(interp, z, n); |
| 839 | 845 | return TH_OK; |
| 840 | 846 | } |
| 841 | 847 | |
| 842 | 848 | /* |
| @@ -919,11 +925,13 @@ | ||
| 919 | 925 | { "is", string_is_command }, |
| 920 | 926 | { "last", string_last_command }, |
| 921 | 927 | { "length", string_length_command }, |
| 922 | 928 | { "range", string_range_command }, |
| 923 | 929 | { "repeat", string_repeat_command }, |
| 924 | - { "trim", string_trim_command }, | |
| 930 | + { "trim", string_trim_command }, | |
| 931 | + { "trimleft", string_trim_command }, | |
| 932 | + { "trimright", string_trim_command }, | |
| 925 | 933 | { 0, 0 } |
| 926 | 934 | }; |
| 927 | 935 | return Th_CallSubCommand(interp, ctx, argc, argv, argl, aSub); |
| 928 | 936 | } |
| 929 | 937 | |
| 930 | 938 |
| --- 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 |