Skip to content

Commit 1b41460

Browse files
author
xiao.dong
committed
add string util case
1 parent ce30233 commit 1b41460

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/iceberg/file_format.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ ICEBERG_EXPORT inline std::string_view ToString(FileFormatType format_type) {
5555
}
5656

5757
/// \brief Convert a string to a FileFormatType
58-
ICEBERG_EXPORT constexpr Result<FileFormatType> FileFormatTypeFromString(
58+
ICEBERG_EXPORT Result<FileFormatType> FileFormatTypeFromString(
5959
std::string_view str) noexcept {
60-
auto lower = internal::StringUtils::to_lower(str);
60+
auto lower = internal::StringUtils::ToLower(str);
6161
if (lower == "parquet") return FileFormatType::kParquet;
6262
if (lower == "avro") return FileFormatType::kAvro;
6363
if (lower == "orc") return FileFormatType::kOrc;

src/iceberg/util/string_utils.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@
2020
#pragma once
2121

2222
#include <algorithm>
23+
#include <ranges>
2324
#include <string>
2425

2526
namespace iceberg::internal {
2627

2728
class StringUtils {
2829
public:
29-
static std::string to_lower(std::string_view str) {
30+
static std::string ToLower(std::string_view str) {
3031
std::string input(str);
31-
std::transform(input.begin(), input.end(), input.begin(),
32-
[](char c) { return std::tolower(c); });
33-
return input;
32+
return input | std::views::transform([](char c) { return std::tolower(c); }) |
33+
std::ranges::to<std::string>();
3434
}
3535

36-
static std::string to_upper(std::string_view str) {
36+
static std::string ToUpper(std::string_view str) {
3737
std::string input(str);
38-
std::transform(input.begin(), input.end(), input.begin(),
39-
[](char c) { return std::toupper(c); });
40-
return input;
38+
return input | std::views::transform([](char c) { return std::toupper(c); }) |
39+
std::ranges::to<std::string>();
4140
}
4241
};
4342

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ target_link_libraries(json_serde_test PRIVATE iceberg_static GTest::gtest_main
6666
add_test(NAME json_serde_test COMMAND json_serde_test)
6767

6868
add_executable(util_test)
69-
target_sources(util_test PRIVATE formatter_test.cc config_test.cc visit_type_test.cc)
69+
target_sources(util_test PRIVATE formatter_test.cc config_test.cc visit_type_test.cc
70+
string_utils_test.cc)
7071
target_link_libraries(util_test PRIVATE iceberg_static GTest::gtest_main GTest::gmock)
7172
add_test(NAME util_test COMMAND util_test)
7273

test/string_utils_test.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "iceberg/util/string_utils.h"
21+
22+
#include <gtest/gtest.h>
23+
24+
namespace iceberg {
25+
26+
TEST(StringUtilsTest, ToLower) {
27+
ASSERT_EQ(internal::StringUtils::ToLower("AbC"), "abc");
28+
ASSERT_EQ(internal::StringUtils::ToLower("A-bC"), "a-bc");
29+
ASSERT_EQ(internal::StringUtils::ToLower("A_bC"), "a_bc");
30+
ASSERT_EQ(internal::StringUtils::ToLower(""), "");
31+
ASSERT_EQ(internal::StringUtils::ToLower(" "), " ");
32+
ASSERT_EQ(internal::StringUtils::ToLower("123"), "123");
33+
}
34+
35+
TEST(StringUtilsTest, ToUpper) {
36+
ASSERT_EQ(internal::StringUtils::ToUpper("abc"), "ABC");
37+
ASSERT_EQ(internal::StringUtils::ToUpper("A-bC"), "A-BC");
38+
ASSERT_EQ(internal::StringUtils::ToUpper("A_bC"), "A_BC");
39+
ASSERT_EQ(internal::StringUtils::ToUpper(""), "");
40+
ASSERT_EQ(internal::StringUtils::ToUpper(" "), " ");
41+
ASSERT_EQ(internal::StringUtils::ToUpper("123"), "123");
42+
}
43+
44+
} // namespace iceberg

0 commit comments

Comments
 (0)