-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce strchrnul implementation and unit tests. Submitting on behalf of Caslyn@ Differential Revision: https://reviews.llvm.org/D147346
- Loading branch information
Showing
16 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//===-- Implementation of strchrnul --------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/string/strchrnul.h" | ||
#include "src/string/string_utils.h" | ||
|
||
#include "src/__support/common.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
LLVM_LIBC_FUNCTION(char *, strchrnul, (const char *src, int c)) { | ||
const char ch = static_cast<char>(c); | ||
for (; *src && *src != ch; ++src) | ||
; | ||
return const_cast<char *>(src); | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===-- Implementation header for strchrnul --------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_STRING_STRCHRNUL_H | ||
#define LLVM_LIBC_SRC_STRING_STRCHRNUL_H | ||
|
||
namespace __llvm_libc { | ||
|
||
char *strchrnul(const char *src, int c); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_STRING_STRCHRNUL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//===-- Unittests for strchrnul -------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/string/strchrnul.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
TEST(LlvmLibcStrChrNulTest, FindsFirstCharacter) { | ||
const char *src = "abcde"; | ||
|
||
// Should return original string since 'a' is the first character. | ||
ASSERT_STREQ(__llvm_libc::strchrnul(src, 'a'), "abcde"); | ||
// Source string should not change. | ||
ASSERT_STREQ(src, "abcde"); | ||
} | ||
|
||
TEST(LlvmLibcStrChrNulTest, FindsMiddleCharacter) { | ||
const char *src = "abcde"; | ||
|
||
// Should return characters after (and including) 'c'. | ||
ASSERT_STREQ(__llvm_libc::strchrnul(src, 'c'), "cde"); | ||
// Source string should not change. | ||
ASSERT_STREQ(src, "abcde"); | ||
} | ||
|
||
TEST(LlvmLibcStrChrNulTest, FindsLastCharacterThatIsNotNullTerminator) { | ||
const char *src = "abcde"; | ||
|
||
// Should return 'e' and null-terminator. | ||
ASSERT_STREQ(__llvm_libc::strchrnul(src, 'e'), "e"); | ||
// Source string should not change. | ||
ASSERT_STREQ(src, "abcde"); | ||
} | ||
|
||
TEST(LlvmLibcStrChrNulTest, FindsNullTerminator) { | ||
const char *src = "abcde"; | ||
|
||
// Should return null terminator. | ||
ASSERT_STREQ(__llvm_libc::strchrnul(src, '\0'), ""); | ||
// Source string should not change. | ||
ASSERT_STREQ(src, "abcde"); | ||
} | ||
|
||
TEST(LlvmLibcStrChrNulTest, | ||
CharacterNotWithinStringShouldReturnNullTerminator) { | ||
const char *src = "123?"; | ||
|
||
// Since 'z' is not within the string, should return a pointer to the source | ||
// string's null terminator. | ||
char *result = __llvm_libc::strchrnul(src, 'z'); | ||
ASSERT_EQ(*result, '\0'); | ||
|
||
char *term = const_cast<char *>(src) + 4; | ||
ASSERT_EQ(result, term); | ||
} | ||
|
||
TEST(LlvmLibcStrChrNulTest, TheSourceShouldNotChange) { | ||
const char *src = "abcde"; | ||
// When the character is found, the source string should not change. | ||
__llvm_libc::strchrnul(src, 'd'); | ||
ASSERT_STREQ(src, "abcde"); | ||
// Same case for when the character is not found. | ||
__llvm_libc::strchrnul(src, 'z'); | ||
ASSERT_STREQ(src, "abcde"); | ||
// Same case for when looking for null terminator. | ||
__llvm_libc::strchrnul(src, '\0'); | ||
ASSERT_STREQ(src, "abcde"); | ||
} | ||
|
||
TEST(LlvmLibcStrChrNulTest, ShouldFindFirstOfDuplicates) { | ||
// '1' is duplicated in the string, but it should find the first copy. | ||
ASSERT_STREQ(__llvm_libc::strchrnul("abc1def1ghi", '1'), "1def1ghi"); | ||
|
||
const char *dups = "XXXXX"; | ||
// Should return original string since 'X' is the first character. | ||
ASSERT_STREQ(__llvm_libc::strchrnul(dups, 'X'), dups); | ||
} | ||
|
||
TEST(LlvmLibcStrChrNulTest, EmptyStringShouldOnlyMatchNullTerminator) { | ||
// Null terminator should match. | ||
ASSERT_STREQ(__llvm_libc::strchrnul("", '\0'), ""); | ||
|
||
// All other characters should not match. | ||
char *result = __llvm_libc::strchrnul("", 'Z'); | ||
ASSERT_EQ(*result, '\0'); | ||
|
||
result = __llvm_libc::strchrnul("", '3'); | ||
ASSERT_EQ(*result, '\0'); | ||
|
||
result = __llvm_libc::strchrnul("", '*'); | ||
ASSERT_EQ(*result, '\0'); | ||
} |