github.heygears.com/openimsdk/tools@v0.0.49/utils/splitter/splitter.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 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 splitter 16 17 // SplitResult holds a slice of strings as a result of the splitting operation. 18 type SplitResult struct { 19 Item []string 20 } 21 22 // Splitter is responsible for splitting a slice of strings into multiple parts. 23 type Splitter struct { 24 splitCount int // The number of parts to split the data into. 25 data []string // The original data to be split. 26 } 27 28 // NewSplitter creates a new Splitter instance with the specified split count and data. 29 func NewSplitter(splitCount int, data []string) *Splitter { 30 return &Splitter{splitCount: splitCount, data: data} 31 } 32 33 // GetSplitResult performs the splitting operation and returns the results as a slice of SplitResult. 34 // Each SplitResult contains a slice of strings, representing a part of the original data. 35 func (s *Splitter) GetSplitResult() (result []*SplitResult) { 36 remain := len(s.data) % s.splitCount 37 integer := len(s.data) / s.splitCount 38 39 for i := 0; i < integer; i++ { 40 r := new(SplitResult) 41 r.Item = s.data[i*s.splitCount : (i+1)*s.splitCount] 42 result = append(result, r) 43 } 44 if remain > 0 { 45 r := new(SplitResult) 46 r.Item = s.data[integer*s.splitCount:] 47 result = append(result, r) 48 } 49 return result 50 }