Fossil SCM
Add [string index] TH1 command
Commit
0c57ba3eb47e2a76aea3e8b0e4fd0ade5e07a67196a0cddb8a9a17f99363ec0a
Parent
00ced6df65596f4…
1 file changed
+48
-14
+48
-14
| --- src/th_lang.c | ||
| +++ src/th_lang.c | ||
| @@ -718,10 +718,39 @@ | ||
| 718 | 718 | return Th_SetResultInt(interp, iRes); |
| 719 | 719 | } |
| 720 | 720 | |
| 721 | 721 | /* |
| 722 | 722 | ** TH Syntax: |
| 723 | +** | |
| 724 | +** string index STRING INDEX | |
| 725 | +*/ | |
| 726 | +static int string_index_command( | |
| 727 | + Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl | |
| 728 | +){ | |
| 729 | + int iIndex; | |
| 730 | + | |
| 731 | + if( argc!=4 ){ | |
| 732 | + return Th_WrongNumArgs(interp, "string index string index"); | |
| 733 | + } | |
| 734 | + | |
| 735 | + if( argl[3]==3 && 0==memcmp("end", argv[3], 3) ){ | |
| 736 | + iIndex = argl[2]; | |
| 737 | + }else if( Th_ToInt(interp, argv[3], argl[3], &iIndex) ){ | |
| 738 | + Th_ErrorMessage( | |
| 739 | + interp, "Expected \"end\" or integer, got:", argv[3], argl[3]); | |
| 740 | + return TH_ERROR; | |
| 741 | + } | |
| 742 | + | |
| 743 | + if( iIndex>=0 && iIndex<argl[2] ){ | |
| 744 | + return Th_SetResult(interp, &argv[2][iIndex], 1); | |
| 745 | + }else{ | |
| 746 | + return Th_SetResult(interp, 0, 0); | |
| 747 | + } | |
| 748 | +} | |
| 749 | + | |
| 750 | +/* | |
| 751 | +** TH Syntax: | |
| 723 | 752 | ** |
| 724 | 753 | ** string is CLASS STRING |
| 725 | 754 | */ |
| 726 | 755 | static int string_is_command( |
| 727 | 756 | Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl |
| @@ -1057,33 +1086,38 @@ | ||
| 1057 | 1086 | } |
| 1058 | 1087 | |
| 1059 | 1088 | /* |
| 1060 | 1089 | ** TH Syntax: |
| 1061 | 1090 | ** |
| 1062 | -** string compare STR1 STR2 | |
| 1063 | -** string first NEEDLE HAYSTACK ?STARTINDEX? | |
| 1064 | -** string is CLASS STRING | |
| 1065 | -** string last NEEDLE HAYSTACK ?STARTINDEX? | |
| 1066 | -** string length STRING | |
| 1067 | -** string range STRING FIRST LAST | |
| 1068 | -** string repeat STRING COUNT | |
| 1091 | +** string compare STR1 STR2 | |
| 1092 | +** string first NEEDLE HAYSTACK ?STARTINDEX? | |
| 1093 | +** string index STRING INDEX | |
| 1094 | +** string is CLASS STRING | |
| 1095 | +** string last NEEDLE HAYSTACK ?STARTINDEX? | |
| 1096 | +** string length STRING | |
| 1097 | +** string range STRING FIRST LAST | |
| 1098 | +** string repeat STRING COUNT | |
| 1099 | +** string trim STRING | |
| 1100 | +** string trimleft STRING | |
| 1101 | +** string trimright STRING | |
| 1069 | 1102 | */ |
| 1070 | 1103 | static int string_command( |
| 1071 | 1104 | Th_Interp *interp, |
| 1072 | 1105 | void *ctx, |
| 1073 | 1106 | int argc, |
| 1074 | 1107 | const char **argv, |
| 1075 | 1108 | int *argl |
| 1076 | 1109 | ){ |
| 1077 | 1110 | static const Th_SubCommand aSub[] = { |
| 1078 | - { "compare", string_compare_command }, | |
| 1079 | - { "first", string_first_command }, | |
| 1080 | - { "is", string_is_command }, | |
| 1081 | - { "last", string_last_command }, | |
| 1082 | - { "length", string_length_command }, | |
| 1083 | - { "range", string_range_command }, | |
| 1084 | - { "repeat", string_repeat_command }, | |
| 1111 | + { "compare", string_compare_command }, | |
| 1112 | + { "first", string_first_command }, | |
| 1113 | + { "index", string_index_command }, | |
| 1114 | + { "is", string_is_command }, | |
| 1115 | + { "last", string_last_command }, | |
| 1116 | + { "length", string_length_command }, | |
| 1117 | + { "range", string_range_command }, | |
| 1118 | + { "repeat", string_repeat_command }, | |
| 1085 | 1119 | { "trim", string_trim_command }, |
| 1086 | 1120 | { "trimleft", string_trim_command }, |
| 1087 | 1121 | { "trimright", string_trim_command }, |
| 1088 | 1122 | { 0, 0 } |
| 1089 | 1123 | }; |
| 1090 | 1124 |
| --- src/th_lang.c | |
| +++ src/th_lang.c | |
| @@ -718,10 +718,39 @@ | |
| 718 | return Th_SetResultInt(interp, iRes); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | ** TH Syntax: |
| 723 | ** |
| 724 | ** string is CLASS STRING |
| 725 | */ |
| 726 | static int string_is_command( |
| 727 | Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl |
| @@ -1057,33 +1086,38 @@ | |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | ** TH Syntax: |
| 1061 | ** |
| 1062 | ** string compare STR1 STR2 |
| 1063 | ** string first NEEDLE HAYSTACK ?STARTINDEX? |
| 1064 | ** string is CLASS STRING |
| 1065 | ** string last NEEDLE HAYSTACK ?STARTINDEX? |
| 1066 | ** string length STRING |
| 1067 | ** string range STRING FIRST LAST |
| 1068 | ** string repeat STRING COUNT |
| 1069 | */ |
| 1070 | static int string_command( |
| 1071 | Th_Interp *interp, |
| 1072 | void *ctx, |
| 1073 | int argc, |
| 1074 | const char **argv, |
| 1075 | int *argl |
| 1076 | ){ |
| 1077 | static const Th_SubCommand aSub[] = { |
| 1078 | { "compare", string_compare_command }, |
| 1079 | { "first", string_first_command }, |
| 1080 | { "is", string_is_command }, |
| 1081 | { "last", string_last_command }, |
| 1082 | { "length", string_length_command }, |
| 1083 | { "range", string_range_command }, |
| 1084 | { "repeat", string_repeat_command }, |
| 1085 | { "trim", string_trim_command }, |
| 1086 | { "trimleft", string_trim_command }, |
| 1087 | { "trimright", string_trim_command }, |
| 1088 | { 0, 0 } |
| 1089 | }; |
| 1090 |
| --- src/th_lang.c | |
| +++ src/th_lang.c | |
| @@ -718,10 +718,39 @@ | |
| 718 | return Th_SetResultInt(interp, iRes); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | ** TH Syntax: |
| 723 | ** |
| 724 | ** string index STRING INDEX |
| 725 | */ |
| 726 | static int string_index_command( |
| 727 | Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl |
| 728 | ){ |
| 729 | int iIndex; |
| 730 | |
| 731 | if( argc!=4 ){ |
| 732 | return Th_WrongNumArgs(interp, "string index string index"); |
| 733 | } |
| 734 | |
| 735 | if( argl[3]==3 && 0==memcmp("end", argv[3], 3) ){ |
| 736 | iIndex = argl[2]; |
| 737 | }else if( Th_ToInt(interp, argv[3], argl[3], &iIndex) ){ |
| 738 | Th_ErrorMessage( |
| 739 | interp, "Expected \"end\" or integer, got:", argv[3], argl[3]); |
| 740 | return TH_ERROR; |
| 741 | } |
| 742 | |
| 743 | if( iIndex>=0 && iIndex<argl[2] ){ |
| 744 | return Th_SetResult(interp, &argv[2][iIndex], 1); |
| 745 | }else{ |
| 746 | return Th_SetResult(interp, 0, 0); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | ** TH Syntax: |
| 752 | ** |
| 753 | ** string is CLASS STRING |
| 754 | */ |
| 755 | static int string_is_command( |
| 756 | Th_Interp *interp, void *ctx, int argc, const char **argv, int *argl |
| @@ -1057,33 +1086,38 @@ | |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | ** TH Syntax: |
| 1090 | ** |
| 1091 | ** string compare STR1 STR2 |
| 1092 | ** string first NEEDLE HAYSTACK ?STARTINDEX? |
| 1093 | ** string index STRING INDEX |
| 1094 | ** string is CLASS STRING |
| 1095 | ** string last NEEDLE HAYSTACK ?STARTINDEX? |
| 1096 | ** string length STRING |
| 1097 | ** string range STRING FIRST LAST |
| 1098 | ** string repeat STRING COUNT |
| 1099 | ** string trim STRING |
| 1100 | ** string trimleft STRING |
| 1101 | ** string trimright STRING |
| 1102 | */ |
| 1103 | static int string_command( |
| 1104 | Th_Interp *interp, |
| 1105 | void *ctx, |
| 1106 | int argc, |
| 1107 | const char **argv, |
| 1108 | int *argl |
| 1109 | ){ |
| 1110 | static const Th_SubCommand aSub[] = { |
| 1111 | { "compare", string_compare_command }, |
| 1112 | { "first", string_first_command }, |
| 1113 | { "index", string_index_command }, |
| 1114 | { "is", string_is_command }, |
| 1115 | { "last", string_last_command }, |
| 1116 | { "length", string_length_command }, |
| 1117 | { "range", string_range_command }, |
| 1118 | { "repeat", string_repeat_command }, |
| 1119 | { "trim", string_trim_command }, |
| 1120 | { "trimleft", string_trim_command }, |
| 1121 | { "trimright", string_trim_command }, |
| 1122 | { 0, 0 } |
| 1123 | }; |
| 1124 |