vvellanki commented on a change in pull request #11287: URL: https://github.com/apache/arrow/pull/11287#discussion_r745310461
########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -794,6 +796,21 @@ const char* gdv_fn_initcap_utf8(int64_t context, const char* data, int32_t data_ *out_len = out_idx; return out; } + +GANDIVA_EXPORT +int32_t gdv_fn_instr_utf8(int64_t context, const char* string, int32_t string_len, + const char* substring, int32_t substring_len) { + if (substring_len == 0) { + return 1; + } + + for(int i = 0; i < string_len; i++) { + if (string[i] == substring[0] && gandiva::string_matches(string + 1, substring + 1, i, substring_len - 1)) { Review comment: Please use memcmp instead - https://www.cplusplus.com/reference/cstring/memcmp/ You are essentially comparing the next substring_len - 1 bytes of both the pointers ########## File path: cpp/src/gandiva/gdv_function_stubs_test.cc ########## @@ -766,4 +766,58 @@ TEST(TestGdvFnStubs, TestCastVarbinaryFloat8) { ctx.Reset(); } +TEST(TestGdvFnStubs, TestInstr) { + gandiva::ExecutionContext ctx; + + int64_t ctx_ptr = reinterpret_cast<int64_t>(&ctx); + + std::string s1 = "hello world!"; + auto s1_len = static_cast<int32_t>(s1.size()); + std::string s2 = "world"; + auto s2_len = static_cast<int32_t>(s2.size()); + + auto result = gdv_fn_instr_utf8(ctx_ptr, s1.c_str(), s1_len, s2.c_str(), s2_len); + EXPECT_EQ(result, 7); + + s1 = "apple, banana, mango"; + s1_len = static_cast<int32_t>(s1.size()); + s2 = "mango"; + s2_len = static_cast<int32_t>(s2.size()); + + result = gdv_fn_instr_utf8(ctx_ptr, s1.c_str(), s1_len, s2.c_str(), s2_len); Review comment: s2="apple" should return 1 ########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -794,6 +796,21 @@ const char* gdv_fn_initcap_utf8(int64_t context, const char* data, int32_t data_ *out_len = out_idx; return out; } + +GANDIVA_EXPORT +int32_t gdv_fn_instr_utf8(int64_t context, const char* string, int32_t string_len, + const char* substring, int32_t substring_len) { + if (substring_len == 0) { + return 1; + } + + for(int i = 0; i < string_len; i++) { Review comment: The condition (i < string_len) is incorrect. Note that you are comparing the next N bytes when the first byte matches. If there is a match at the last byte, you will access beyond the end here and cause issues This should be changed to (i < string_len - substring_len) ########## File path: cpp/src/gandiva/string_utils.cc ########## @@ -0,0 +1,33 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "string_utils.h" + +namespace gandiva { +GANDIVA_EXPORT +bool string_matches(const char *string, const char *substring, int start_idx, int substring_len) { Review comment: This is not required, please use memcmp() instead -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org