@@ -1036,6 +1036,81 @@ static int _parse_function_exists(char *in, int *result, int *parsed_chars) {
1036
1036
}
1037
1037
1038
1038
1039
+ static int _parse_function_substring (char * in , struct stack_item * si , int * parsed_chars ) {
1040
+
1041
+ int i , j , res , old_expect = g_expect_calculations , source_index_original = g_source_index , source_index_backup , index , length ;
1042
+ char tmp [MAX_NAME_LENGTH + 1 ];
1043
+
1044
+ /* NOTE! we assume that 'in' is actually '&g_buffer[xyz]', so
1045
+ let's update g_source_index for input_number() */
1046
+
1047
+ g_source_index = (int )(in - g_buffer );
1048
+ source_index_backup = g_source_index ;
1049
+
1050
+ /* string */
1051
+ g_expect_calculations = NO ;
1052
+ res = input_number ();
1053
+
1054
+ if (res != INPUT_NUMBER_ADDRESS_LABEL && res != INPUT_NUMBER_STRING ) {
1055
+ print_error (ERROR_NUM , "substring() requires a string to operate on.\n" );
1056
+ return FAILED ;
1057
+ }
1058
+
1059
+ strcpy (tmp , g_label );
1060
+
1061
+ /* index */
1062
+ g_expect_calculations = YES ;
1063
+ res = input_number ();
1064
+
1065
+ if (res != SUCCEEDED ) {
1066
+ print_error (ERROR_NUM , "substring() requires index that can be solved right here.\n" );
1067
+ return FAILED ;
1068
+ }
1069
+
1070
+ index = g_parsed_int ;
1071
+
1072
+ /* length */
1073
+ res = input_number ();
1074
+ g_expect_calculations = old_expect ;
1075
+
1076
+ if (res != SUCCEEDED ) {
1077
+ print_error (ERROR_NUM , "substring() requires length that can be solved right here.\n" );
1078
+ return FAILED ;
1079
+ }
1080
+
1081
+ length = g_parsed_int ;
1082
+
1083
+ if (g_buffer [g_source_index ] != ')' ) {
1084
+ print_error (ERROR_NUM , "Malformed \"substring(?)\" detected!\n" );
1085
+ return FAILED ;
1086
+ }
1087
+
1088
+ /* skip ')' */
1089
+ g_source_index ++ ;
1090
+
1091
+ /* count the parsed chars */
1092
+ * parsed_chars = (int )(g_source_index - source_index_backup );
1093
+
1094
+ /* return g_source_index */
1095
+ g_source_index = source_index_original ;
1096
+
1097
+ /* perform substring() */
1098
+ si -> sign = SI_SIGN_POSITIVE ;
1099
+ si -> type = STACK_ITEM_TYPE_STRING ;
1100
+
1101
+ for (j = 0 , i = index ; j < length ; i ++ , j ++ ) {
1102
+ if (i < 0 || i >= (int )strlen (tmp )) {
1103
+ print_error (ERROR_NUM , "Index %d is outside string \"%s\"!\n" , i , tmp );
1104
+ return FAILED ;
1105
+ }
1106
+ si -> string [j ] = tmp [i ];
1107
+ }
1108
+ si -> string [j ] = 0 ;
1109
+
1110
+ return SUCCEEDED ;
1111
+ }
1112
+
1113
+
1039
1114
static int _parse_function_math1 (char * in , int * type , double * value , char * string , int * parsed_chars , char * name ) {
1040
1115
1041
1116
int res , source_index_original = g_source_index , source_index_backup , input_float_mode = g_input_float_mode ;
@@ -1826,7 +1901,7 @@ static int _stack_calculate(char *in, int *value, int *bytes_parsed, unsigned ch
1826
1901
can_skip_newline = YES ;
1827
1902
/* was previous token ')'? */
1828
1903
if (q > 0 && si [q - 1 ].type == STACK_ITEM_TYPE_OPERATOR && si [q - 1 ].value == SI_OP_RIGHT )
1829
- break ;
1904
+ break ;
1830
1905
q ++ ;
1831
1906
b ++ ;
1832
1907
in ++ ;
@@ -2567,10 +2642,20 @@ static int _stack_calculate(char *in, int *value, int *bytes_parsed, unsigned ch
2567
2642
if (_parse_function_exists (in , & d , & parsed_chars ) == FAILED )
2568
2643
return FAILED ;
2569
2644
in += parsed_chars ;
2570
- is_label = NO ;
2645
+ is_label = NO ;
2571
2646
break ;
2572
2647
}
2648
+ else if (k == 9 && strcaselesscmpn (si [q ].string , "substring(" , 10 ) == 0 ) {
2649
+ int parsed_chars = 0 ;
2573
2650
2651
+ if (_parse_function_substring (in , & si [q ], & parsed_chars ) == FAILED )
2652
+ return FAILED ;
2653
+ in += parsed_chars ;
2654
+ is_label = NO ;
2655
+ is_already_processed_function = YES ;
2656
+ break ;
2657
+ }
2658
+
2574
2659
if (e == '(' ) {
2575
2660
/* are we calling a user created function? */
2576
2661
int found_function = NO , res , parsed_chars = 0 ;
@@ -2739,6 +2824,7 @@ static int _stack_calculate(char *in, int *value, int *bytes_parsed, unsigned ch
2739
2824
2740
2825
return SUCCEEDED ;
2741
2826
}
2827
+
2742
2828
if (from_substitutor == NO ) {
2743
2829
if (si [0 ].type == STACK_ITEM_TYPE_STACK ) {
2744
2830
/* update the source pointer */
@@ -2749,14 +2835,29 @@ static int _stack_calculate(char *in, int *value, int *bytes_parsed, unsigned ch
2749
2835
return INPUT_NUMBER_STACK ;
2750
2836
}
2751
2837
else if (got_get_label == YES && si [0 ].type == STACK_ITEM_TYPE_LABEL && si [0 ].sign == SI_SIGN_POSITIVE ) {
2838
+ /* update the source pointer */
2752
2839
* bytes_parsed += (int )(in - in_original ) - 1 ;
2753
2840
2754
2841
strcpy (g_label , si [0 ].string );
2755
2842
process_special_labels (g_label );
2843
+ g_string_size = (int )strlen (g_label );
2756
2844
2757
2845
return STACK_RETURN_LABEL ;
2758
2846
}
2847
+ else if (si [0 ].type == STACK_ITEM_TYPE_STRING && si [0 ].sign == SI_SIGN_POSITIVE ) {
2848
+ /* update the source pointer */
2849
+ * bytes_parsed += (int )(in - in_original ) - 1 ;
2759
2850
2851
+ strcpy (g_label , si [0 ].string );
2852
+ process_special_labels (g_label );
2853
+ g_string_size = (int )strlen (g_label );
2854
+
2855
+ #if defined(WLA_DEBUG )
2856
+ print_text (NO , "RETURN STRING %s\n" , g_label );
2857
+ #endif
2858
+ return STACK_RETURN_STRING ;
2859
+ }
2860
+
2760
2861
return STACK_CALCULATE_DELAY ;
2761
2862
}
2762
2863
}
@@ -2984,7 +3085,7 @@ static int _stack_calculate(char *in, int *value, int *bytes_parsed, unsigned ch
2984
3085
}
2985
3086
}
2986
3087
}
2987
-
3088
+
2988
3089
#if defined(WLA_DEBUG )
2989
3090
print_text (NO , "INFIX:\n" );
2990
3091
_debug_print_stack (g_active_file_info_last -> line_current , -1 , si , q , 0 , NULL );
0 commit comments