Marks as IR_ALWAYS_INLINE since this is called in other overloads. So the overloads can also replace GetConstFnAttr() calls in codegen.
| 887 | // Marks as IR_ALWAYS_INLINE since this is called in other overloads. |
| 888 | // So the overloads can also replace GetConstFnAttr() calls in codegen. |
| 889 | IR_ALWAYS_INLINE IntVal StringFunctions::Instr(FunctionContext* context, |
| 890 | const StringVal& str, const StringVal& substr, const BigIntVal& start_position, |
| 891 | const BigIntVal& occurrence) { |
| 892 | if (str.is_null || substr.is_null || start_position.is_null || occurrence.is_null) { |
| 893 | return IntVal::null(); |
| 894 | } |
| 895 | if (occurrence.val <= 0) { |
| 896 | stringstream ss; |
| 897 | ss << "Invalid occurrence parameter to instr function: " << occurrence.val; |
| 898 | context->SetError(ss.str().c_str()); |
| 899 | return IntVal(0); |
| 900 | } |
| 901 | if (start_position.val == 0) return IntVal(0); |
| 902 | |
| 903 | bool utf8_mode = context->impl()->GetConstFnAttr(FunctionContextImpl::UTF8_MODE); |
| 904 | StringValue haystack = StringValue::FromStringVal(str); |
| 905 | StringValue::SimpleString haystack_s = haystack.ToSimpleString(); |
| 906 | StringValue needle = StringValue::FromStringVal(substr); |
| 907 | StringValue::SimpleString needle_s = needle.ToSimpleString(); |
| 908 | StringSearch search(&needle); |
| 909 | int match_pos = -1; |
| 910 | if (start_position.val > 0) { |
| 911 | // A positive starting position indicates regular searching from the left. |
| 912 | int search_start_pos = start_position.val - 1; |
| 913 | if (utf8_mode) { |
| 914 | search_start_pos = FindUtf8PosForward(str.ptr, str.len, search_start_pos); |
| 915 | } |
| 916 | if (search_start_pos >= haystack_s.len) return IntVal(0); |
| 917 | for (int match_num = 0; match_num < occurrence.val; ++match_num) { |
| 918 | DCHECK_LE(search_start_pos, haystack_s.len); |
| 919 | StringValue haystack_substring = haystack.Substring(search_start_pos); |
| 920 | int match_pos_in_substring = search.Search(&haystack_substring); |
| 921 | if (match_pos_in_substring < 0) return IntVal(0); |
| 922 | match_pos = search_start_pos + match_pos_in_substring; |
| 923 | search_start_pos = match_pos + 1; |
| 924 | } |
| 925 | } else { |
| 926 | // A negative starting position indicates searching from the right. |
| 927 | int search_start_pos = utf8_mode ? |
| 928 | FindUtf8PosBackward(str.ptr, str.len, -start_position.val - 1) : |
| 929 | haystack_s.len + start_position.val; |
| 930 | // The needle must fit between search_start_pos and the end of the string |
| 931 | if (search_start_pos + needle_s.len > haystack_s.len) { |
| 932 | search_start_pos = haystack_s.len - needle_s.len; |
| 933 | } |
| 934 | if (search_start_pos < 0) return IntVal(0); |
| 935 | for (int match_num = 0; match_num < occurrence.val; ++match_num) { |
| 936 | DCHECK_GE(search_start_pos + needle_s.len, 0); |
| 937 | DCHECK_LE(search_start_pos + needle_s.len, haystack_s.len); |
| 938 | StringValue haystack_substring = |
| 939 | haystack.Substring(0, search_start_pos + needle_s.len); |
| 940 | match_pos = search.RSearch(&haystack_substring); |
| 941 | if (match_pos < 0) return IntVal(0); |
| 942 | search_start_pos = match_pos - 1; |
| 943 | } |
| 944 | } |
| 945 | // In UTF8 mode, positions are counted by Unicode characters in UTF8 encoding. |
| 946 | // If not in UTF8 mode, return positions starting from 1 at the leftmost position. |
nothing calls this directly
no test coverage detected