github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/util/writer/int_writer.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package writer 22 23 // IntLength determines the number of digits in a base 10 integer. 24 func IntLength(i int) int { 25 if i == 0 { 26 return 1 27 } 28 29 count := 0 30 for ; i > 0; i /= 10 { 31 count++ 32 } 33 34 return count 35 } 36 37 // WriteInteger writes a base 10 integer to a buffer at a given index. 38 // 39 // NB: based on fmt.Printf handling of integers, specifically base 10 case. 40 func WriteInteger(dst []byte, value, idx int) int { 41 // Because printing is easier right-to-left: format u into buf, ending at buf[i]. 42 // We could make things marginally faster by splitting the 32-bit case out 43 // into a separate block but it's not worth the duplication, so u has 64 bits. 44 // Use constants for the division and modulo for more efficient code. 45 // Switch cases ordered by popularity. 46 idx = idx + IntLength(value) 47 finalIndex := idx 48 for value >= 10 { 49 idx-- 50 dst[idx] = byte(48 + value%10) 51 next := value / 10 52 value = next 53 } 54 55 dst[idx-1] = byte(48 + value) 56 return finalIndex 57 } 58 59 // IntsLength determines the number of digits in a list of base 10 integers, 60 // accounting for separators between each integer. 61 func IntsLength(is []int) int { 62 // initialize length accounting for separators. 63 l := len(is) - 1 64 for _, i := range is { 65 l += IntLength(i) 66 } 67 68 return l 69 } 70 71 // WriteIntegers writes a slice of base 10 integer to a buffer at a given index, 72 // separating each value with the given separator, returning the index at which 73 // the write ends. 74 // 75 // NB: Ensure that there is sufficient space in the buffer to hold values and 76 // separators. 77 func WriteIntegers(dst []byte, values []int, sep byte, idx int) int { 78 l := len(values) - 1 79 for _, v := range values[:l] { 80 idx = WriteInteger(dst, v, idx) 81 dst[idx] = sep 82 idx++ 83 } 84 85 idx = WriteInteger(dst, values[l], idx) 86 // Write the last integer. 87 return idx 88 }