Skip to content

Commit 5570a5a

Browse files
committed
chore: use more from_chars
1 parent f2d0abd commit 5570a5a

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

src/iceberg/avro/avro_schema_util.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* under the License.
1818
*/
1919

20+
#include <charconv>
2021
#include <format>
2122
#include <mutex>
2223
#include <string_view>
@@ -413,11 +414,13 @@ Result<int32_t> GetId(const ::avro::NodePtr& node, const std::string& attr_name,
413414
return InvalidSchema("Missing avro attribute: {}", attr_name);
414415
}
415416

416-
try {
417-
return std::stoi(id_str.value());
418-
} catch (const std::exception& e) {
419-
return InvalidSchema("Invalid {}: {}", attr_name, id_str.value());
417+
int32_t id;
418+
const auto& id_value = id_str.value();
419+
auto [_, ec] = std::from_chars(id_value.data(), id_value.data() + id_value.size(), id);
420+
if (ec != std::errc()) {
421+
return InvalidSchema("Invalid {}: {}", attr_name, id_value);
420422
}
423+
return id;
421424
}
422425

423426
Result<int32_t> GetElementId(const ::avro::NodePtr& node) {

src/iceberg/schema_internal.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "iceberg/schema_internal.h"
2121

22+
#include <charconv>
2223
#include <cstring>
2324
#include <optional>
2425
#include <string>
@@ -192,7 +193,11 @@ int32_t GetFieldId(const ArrowSchema& schema) {
192193
return kUnknownFieldId;
193194
}
194195

195-
return std::stoi(std::string(field_id_value.data, field_id_value.size_bytes));
196+
int32_t field_id = kUnknownFieldId;
197+
std::from_chars(field_id_value.data, field_id_value.data + field_id_value.size_bytes,
198+
field_id);
199+
200+
return field_id;
196201
}
197202

198203
Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {

test/config_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
* under the License.
1818
*/
1919

20+
#include "iceberg/util/config.h"
21+
2022
#include <string>
2123

2224
#include <gtest/gtest.h>
23-
#include <iceberg/util/config.h>
2425

2526
namespace iceberg {
2627

test/partition_field_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#include <format>
2323

2424
#include <gtest/gtest.h>
25-
#include <iceberg/type.h>
2625

2726
#include "iceberg/transform.h"
27+
#include "iceberg/type.h"
2828
#include "iceberg/util/formatter.h" // IWYU pragma: keep
2929

3030
namespace iceberg {

test/partition_spec_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
#include <gmock/gmock.h>
2626
#include <gtest/gtest.h>
27-
#include <iceberg/schema_field.h>
2827

2928
#include "iceberg/partition_field.h"
3029
#include "iceberg/schema.h"
30+
#include "iceberg/schema_field.h"
3131
#include "iceberg/transform.h"
3232
#include "iceberg/util/formatter.h" // IWYU pragma: keep
3333

0 commit comments

Comments
 (0)