github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/subStrIndex/subStr_Index.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package substrindex 16 17 import ( 18 "strings" 19 ) 20 21 // function subStrIndex is used to return a substring from a string 22 //before a specified number of occurrences of the delimiter. 23 //------------------------------------------------------------------------- 24 // param1 str: The original string from which we want to create a substring. 25 // param2 delim: Is a string that acts as a delimiter. 26 // param3 count: It identifies the number of times to search for the delimiter. It can be both a positive or negative number. 27 // If it is a positive number, this function returns all to the left of the delimiter. 28 // If it is a negative number, this function returns all to the right of the delimiter. 29 30 func subStrIndex(str, delim string, count int64) (string, error) { 31 // if the length of delim is 0, return empty string 32 if len(delim) == 0 { 33 return "", nil 34 } 35 // if the count is 0, return empty string 36 if count == 0 { 37 return "", nil 38 } 39 40 partions := strings.Split(str, delim) 41 start, end := int64(0), int64(len(partions)) 42 43 if count > 0 { 44 //is count is positive, reset the end position 45 if count < end { 46 end = count 47 } 48 } else { 49 count = -count 50 51 // -count overflows max int64, return the whole string. 52 if count < 0 { 53 return str, nil 54 } 55 56 //if count is negative, reset the start postion 57 if count < end { 58 start = end - count 59 } 60 } 61 subPartions := partions[start:end] 62 return strings.Join(subPartions, delim), nil 63 } 64 65 func SubStrIndex(strs, delims []string, counts []int64, rowCount int, constVectors []bool, results []string) { 66 if constVectors[0] { 67 str := strs[0] 68 if constVectors[1] { 69 delim := delims[0] 70 if constVectors[2] { 71 //scalar - scalar - scalar 72 results[0], _ = subStrIndex(str, delim, counts[0]) 73 } else { 74 //scalar - scalar - vector 75 for i := 0; i < rowCount; i++ { 76 results[i], _ = subStrIndex(str, delim, counts[i]) 77 } 78 } 79 } else { 80 if constVectors[2] { 81 count := counts[0] 82 //scalar - vector - scalar 83 for i := 0; i < rowCount; i++ { 84 results[i], _ = subStrIndex(str, delims[i], count) 85 } 86 } else { 87 //scalar - vector - vector 88 for i := 0; i < rowCount; i++ { 89 results[i], _ = subStrIndex(str, delims[i], counts[i]) 90 } 91 } 92 } 93 } else { 94 if constVectors[1] { 95 delim := delims[0] 96 if constVectors[2] { 97 count := counts[0] 98 //vector - scalar - scalar 99 for i := 0; i < rowCount; i++ { 100 results[i], _ = subStrIndex(strs[i], delim, count) 101 } 102 } else { 103 //vaetor - scalar - vector 104 for i := 0; i < rowCount; i++ { 105 results[i], _ = subStrIndex(strs[i], delim, counts[i]) 106 } 107 } 108 } else { 109 if constVectors[2] { 110 count := counts[0] 111 //vector - vector - scalar 112 for i := 0; i < rowCount; i++ { 113 results[i], _ = subStrIndex(strs[i], delims[i], count) 114 } 115 } else { 116 //vector - vector - vector 117 for i := 0; i < rowCount; i++ { 118 results[i], _ = subStrIndex(strs[i], delims[i], counts[i]) 119 } 120 } 121 } 122 } 123 }