Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
string.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_UTIL_STRING_UTIL_H
19 #define ARROW_UTIL_STRING_UTIL_H
20 
21 #include <algorithm>
22 #include <string>
23 
24 #include "arrow/status.h"
25 
26 namespace arrow {
27 
28 static const char* kAsciiTable = "0123456789ABCDEF";
29 
30 static inline std::string HexEncode(const uint8_t* data, int32_t length) {
31  std::string hex_string;
32  hex_string.reserve(length * 2);
33  for (int32_t j = 0; j < length; ++j) {
34  // Convert to 2 base16 digits
35  hex_string.push_back(kAsciiTable[data[j] >> 4]);
36  hex_string.push_back(kAsciiTable[data[j] & 15]);
37  }
38  return hex_string;
39 }
40 
41 static inline Status ParseHexValue(const char* data, uint8_t* out) {
42  char c1 = data[0];
43  char c2 = data[1];
44 
45  const char* pos1 = std::lower_bound(kAsciiTable, kAsciiTable + 16, c1);
46  const char* pos2 = std::lower_bound(kAsciiTable, kAsciiTable + 16, c2);
47 
48  // Error checking
49  if (*pos1 != c1 || *pos2 != c2) {
50  return Status::Invalid("Encountered non-hex digit");
51  }
52 
53  *out = static_cast<uint8_t>((pos1 - kAsciiTable) << 4 | (pos2 - kAsciiTable));
54  return Status::OK();
55 }
56 
57 } // namespace arrow
58 
59 #endif // ARROW_UTIL_STRING_UTIL_H
static Status OK()
Definition: status.h:119
static Status Invalid(const std::string &msg)
Definition: status.h:142
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29