mapleFU commented on code in PR #242: URL: https://github.com/apache/iceberg-cpp/pull/242#discussion_r2385377572
########## src/iceberg/util/uuid.cc: ########## @@ -0,0 +1,219 @@ +/* + * 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 "iceberg/util/uuid.h" + +#include <chrono> +#include <cstdint> +#include <cstring> +#include <random> +#include <string> + +#include "iceberg/exception.h" +#include "iceberg/result.h" +#include "iceberg/util/int128.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace { + +constexpr std::array<uint8_t, 256> BuildHexTable() { + std::array<uint8_t, 256> buf{}; + for (int32_t i = 0; i < 256; i++) { + if (i >= '0' && i <= '9') { + buf[i] = static_cast<uint8_t>(i - '0'); + } else if (i >= 'a' && i <= 'f') { + buf[i] = static_cast<uint8_t>(i - 'a' + 10); + } else if (i >= 'A' && i <= 'F') { + buf[i] = static_cast<uint8_t>(i - 'A' + 10); + } else { + buf[i] = 0xFF; + } + } + return buf; +} + +constexpr std::array<uint8_t, 256> BuildShl4Table() { + std::array<uint8_t, 256> buf{}; + for (int32_t i = 0; i < 256; i++) { + buf[i] = static_cast<uint8_t>(i << 4); + } + return buf; +} + +constexpr auto kHexTable = BuildHexTable(); +constexpr auto kShl4Table = BuildShl4Table(); + +// Parse a UUID string without dashes, e.g. "67e5504410b1426f9247bb680e5fe0c8" +inline Result<Uuid> ParseSimple(std::string_view s) { + ICEBERG_DCHECK(s.size() == 32, "s must be 32 characters long"); + + std::array<uint8_t, 16> uuid{}; + for (size_t i = 0; i < 16; i++) { + uint8_t h1 = kHexTable[static_cast<uint8_t>(s[i * 2])]; + uint8_t h2 = kHexTable[static_cast<uint8_t>(s[i * 2 + 1])]; + + if ((h1 | h2) == 0xFF) { + return InvalidArgument("Invalid UUID string: {}", s); + } + + uuid[i] = static_cast<uint8_t>(kShl4Table[h1] | h2); + } + return Uuid(std::move(uuid)); +} + +// Parse a UUID string with dashes, e.g. "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +inline Result<Uuid> ParseHyphenated(std::string_view s) { + ICEBERG_DCHECK(s.size() == 36, "s must be 36 characters long"); + + // Check that dashes are in the right places + if (!(s[8] == '-' && s[13] == '-' && s[18] == '-' && s[23] == '-')) { + return InvalidArgument("Invalid UUID string: {}", s); + } + + constexpr std::array<size_t, 8> positions = {0, 4, 9, 14, 19, 24, 28, 32}; + std::array<uint8_t, 16> uuid{}; + + for (size_t j = 0; j < 8; j++) { + size_t i = positions[j]; + uint8_t h1 = kHexTable[static_cast<uint8_t>(s[i])]; + uint8_t h2 = kHexTable[static_cast<uint8_t>(s[i + 1])]; + uint8_t h3 = kHexTable[static_cast<uint8_t>(s[i + 2])]; + uint8_t h4 = kHexTable[static_cast<uint8_t>(s[i + 3])]; + + if ((h1 | h2 | h3 | h4) == 0xFF) { Review Comment: Unlikely? ########## src/iceberg/util/uuid.cc: ########## @@ -0,0 +1,219 @@ +/* + * 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 "iceberg/util/uuid.h" + +#include <chrono> +#include <cstdint> +#include <cstring> +#include <random> +#include <string> + +#include "iceberg/exception.h" +#include "iceberg/result.h" +#include "iceberg/util/int128.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace { + +constexpr std::array<uint8_t, 256> BuildHexTable() { + std::array<uint8_t, 256> buf{}; + for (int32_t i = 0; i < 256; i++) { + if (i >= '0' && i <= '9') { + buf[i] = static_cast<uint8_t>(i - '0'); + } else if (i >= 'a' && i <= 'f') { + buf[i] = static_cast<uint8_t>(i - 'a' + 10); + } else if (i >= 'A' && i <= 'F') { + buf[i] = static_cast<uint8_t>(i - 'A' + 10); + } else { + buf[i] = 0xFF; + } + } + return buf; +} + +constexpr std::array<uint8_t, 256> BuildShl4Table() { + std::array<uint8_t, 256> buf{}; + for (int32_t i = 0; i < 256; i++) { + buf[i] = static_cast<uint8_t>(i << 4); + } + return buf; +} + +constexpr auto kHexTable = BuildHexTable(); +constexpr auto kShl4Table = BuildShl4Table(); + +// Parse a UUID string without dashes, e.g. "67e5504410b1426f9247bb680e5fe0c8" +inline Result<Uuid> ParseSimple(std::string_view s) { + ICEBERG_DCHECK(s.size() == 32, "s must be 32 characters long"); + + std::array<uint8_t, 16> uuid{}; + for (size_t i = 0; i < 16; i++) { + uint8_t h1 = kHexTable[static_cast<uint8_t>(s[i * 2])]; + uint8_t h2 = kHexTable[static_cast<uint8_t>(s[i * 2 + 1])]; + + if ((h1 | h2) == 0xFF) { Review Comment: Unlikely? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
