Skip to content

Commit 2f949db

Browse files
committed
introduce rcutils_strcasecmp, case insensitive string compare.
Signed-off-by: Tomoya.Fujita <[email protected]>
1 parent af3ac24 commit 2f949db

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ set(rcutils_sources
6464
src/repl_str.c
6565
src/shared_library.c
6666
src/snprintf.c
67+
src/strcasecmp.c
6768
src/split.c
6869
src/strdup.c
6970
src/strerror.c
@@ -401,6 +402,13 @@ if(BUILD_TESTING)
401402
target_link_libraries(test_snprintf ${PROJECT_NAME})
402403
endif()
403404

405+
rcutils_custom_add_gtest(test_strcasecmp
406+
test/test_strcasecmp.cpp
407+
)
408+
if(TARGET test_strcasecmp)
409+
target_link_libraries(test_strcasecmp ${PROJECT_NAME})
410+
endif()
411+
404412
rcutils_custom_add_gtest(test_uint8_array
405413
test/test_uint8_array.cpp
406414
)

include/rcutils/strcasecmp.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef RCUTILS__STRCASECMP_H_
16+
#define RCUTILS__STRCASECMP_H_
17+
18+
#ifdef __cplusplus
19+
extern "C"
20+
{
21+
#endif
22+
23+
#include <string.h>
24+
25+
#include "rcutils/macros.h"
26+
#include "rcutils/visibility_control.h"
27+
28+
/// Case insensitive string compare.
29+
/**
30+
* This function compares two strings ignoring case which just wraps strcasecmp()
31+
* or stricmp() in a portable way. This performs a byte-by-byte comparison of the
32+
* strings s1 and s2, ignoring the case of the characters.
33+
*
34+
* \param[in] s1 String with null terminated to compare.
35+
* \param[in] s2 String with null terminated to compare.
36+
* \return An integer less than, equal to, or greater than zero if s1 is, after
37+
* ignoring case, found to be less than, to match, or be greater than s2,
38+
* respectively.
39+
*/
40+
RCUTILS_PUBLIC
41+
RCUTILS_WARN_UNUSED
42+
int
43+
rcutils_strcasecmp(const char * s1, const char * s2);
44+
45+
#ifdef __cplusplus
46+
}
47+
#endif
48+
49+
#endif // RCUTILS__STRCASECMP_H_

src/strcasecmp.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2020 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifdef __cplusplus
16+
extern "C"
17+
{
18+
#endif
19+
20+
#include "rcutils/strcasecmp.h"
21+
22+
int
23+
rcutils_strcasecmp(const char * s1, const char * s2)
24+
{
25+
#ifndef _WIN32
26+
return strcasecmp(s1, s2);
27+
#else
28+
return stricmp(s1, s2);
29+
#endif
30+
}
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif

test/test_strcasecmp.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2020 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <gtest/gtest.h>
16+
17+
#include "rcutils/strcasecmp.h"
18+
19+
// Tests the rcutils_strcasecmp() function.
20+
TEST(TestStrcasecmp, test_strcasecmp) {
21+
EXPECT_EQ(0, rcutils_strcasecmp("", ""));
22+
EXPECT_EQ(0, rcutils_strcasecmp("abc", "abc"));
23+
EXPECT_EQ(0, rcutils_strcasecmp("ABC", "ABC"));
24+
EXPECT_EQ(0, rcutils_strcasecmp("1abc", "1abc"));
25+
EXPECT_EQ(0, rcutils_strcasecmp("abc1", "abc1"));
26+
EXPECT_EQ(0, rcutils_strcasecmp("1ABC", "1ABC"));
27+
EXPECT_EQ(0, rcutils_strcasecmp("ABC1", "ABC1"));
28+
EXPECT_EQ(0, rcutils_strcasecmp("ABC", "abc"));
29+
EXPECT_EQ(0, rcutils_strcasecmp("abc", "ABC"));
30+
EXPECT_EQ(0, rcutils_strcasecmp("Abc", "abc"));
31+
EXPECT_EQ(0, rcutils_strcasecmp("abc", "Abc"));
32+
EXPECT_EQ(0, rcutils_strcasecmp("Abc", "Abc"));
33+
EXPECT_EQ(0, rcutils_strcasecmp("aBc", "abc"));
34+
EXPECT_EQ(0, rcutils_strcasecmp("abc", "aBc"));
35+
EXPECT_EQ(0, rcutils_strcasecmp("aBc", "aBc"));
36+
EXPECT_EQ(0, rcutils_strcasecmp("abC", "abc"));
37+
EXPECT_EQ(0, rcutils_strcasecmp("abc", "abC"));
38+
EXPECT_EQ(0, rcutils_strcasecmp("abC", "abC"));
39+
40+
EXPECT_NE(0, rcutils_strcasecmp("", "abc"));
41+
EXPECT_NE(0, rcutils_strcasecmp("abc", ""));
42+
EXPECT_NE(0, rcutils_strcasecmp("abcd", "abc"));
43+
EXPECT_NE(0, rcutils_strcasecmp("abc", "abcd"));
44+
EXPECT_NE(0, rcutils_strcasecmp("abcD", "abc"));
45+
EXPECT_NE(0, rcutils_strcasecmp("abc", "abcD"));
46+
EXPECT_NE(0, rcutils_strcasecmp("1abc", "abc"));
47+
EXPECT_NE(0, rcutils_strcasecmp("abc", "1abc"));
48+
EXPECT_NE(0, rcutils_strcasecmp("abc1", "abc"));
49+
EXPECT_NE(0, rcutils_strcasecmp("abc", "abc1"));
50+
EXPECT_NE(0, rcutils_strcasecmp("ABCd", "abc"));
51+
EXPECT_NE(0, rcutils_strcasecmp("abc", "ABCd"));
52+
EXPECT_NE(0, rcutils_strcasecmp("1Abc", "abc"));
53+
EXPECT_NE(0, rcutils_strcasecmp("abc", "1Abc"));
54+
EXPECT_NE(0, rcutils_strcasecmp("a1Bc", "abc"));
55+
EXPECT_NE(0, rcutils_strcasecmp("abc", "a1Bc"));
56+
EXPECT_NE(0, rcutils_strcasecmp("ab1C", "abc"));
57+
EXPECT_NE(0, rcutils_strcasecmp("abc", "ab1C"));
58+
}

0 commit comments

Comments
 (0)