-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] wcsstr implementation #142440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[libc] wcsstr implementation #142440
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
//===-- Implementation of wcsstr ------------------------------------------===// | ||
// | ||
// 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/wchar/wcsstr.h" | ||
|
||
#include "hdr/types/size_t.h" | ||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/string/string_utils.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(const wchar_t *, wcsstr, | ||
(const wchar_t *s1, const wchar_t *s2)) { | ||
size_t s1_len = internal::string_length(s1); | ||
size_t s2_len = internal::string_length(s2); | ||
if (s2_len == 0) | ||
return s1; | ||
if (s2_len > s1_len) | ||
return nullptr; | ||
for (size_t i = 0; i <= (s1_len - s2_len); ++i) { | ||
size_t j = 0; | ||
// j will increment until the characters don't match or end of string. | ||
for (; j < s2_len && s1[i + j] == s2[j]; ++j) | ||
; | ||
if (j == s2_len) | ||
return (s1 + i); | ||
} | ||
return nullptr; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or 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,21 @@ | ||
//===-- Implementation header for wcsstr ----------------------------------===// | ||
// | ||
// 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_WCHAR_WCSSTR_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCSSTR_H | ||
|
||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
const wchar_t *wcsstr(const wchar_t *s1, const wchar_t *s2); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCSSTR_H |
This file contains hidden or 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 hidden or 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,113 @@ | ||
//===-- Unittests for wcsstr ----------------------------------------------===// | ||
// | ||
// 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 "hdr/types/wchar_t.h" | ||
#include "src/wchar/wcsstr.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedleNotInHaystack) { | ||
// Should return nullptr if string is not found. | ||
const wchar_t *haystack = L"12345"; | ||
const wchar_t *needle = L"a"; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedleIsEmptyString) { | ||
// Should return pointer to first character if needle is empty. | ||
const wchar_t *haystack = L"12345"; | ||
const wchar_t *needle = L""; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, HaystackIsEmptyString) { | ||
// Should return nullptr since haystack is empty. | ||
const wchar_t *needle = L"12345"; | ||
const wchar_t *haystack = L""; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, HaystackAndNeedleAreEmptyStrings) { | ||
// Should point to haystack since needle is empty. | ||
const wchar_t *needle = L""; | ||
const wchar_t *haystack = L""; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, HaystackAndNeedleAreSingleCharacters) { | ||
const wchar_t *haystack = L"a"; | ||
// Should point to haystack. | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"a"), haystack); | ||
// Should return nullptr. | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"b"), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedleEqualToHaystack) { | ||
const wchar_t *haystack = L"12345"; | ||
// Should point to haystack. | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"12345"), haystack); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedleLargerThanHaystack) { | ||
const wchar_t *haystack = L"123"; | ||
// Should return nullptr. | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"12345"), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedleAtBeginning) { | ||
const wchar_t *haystack = L"12345"; | ||
const wchar_t *needle = L"12"; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedleInMiddle) { | ||
const wchar_t *haystack = L"abcdefghi"; | ||
const wchar_t *needle = L"def"; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 3); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedleDirectlyBeforeNullTerminator) { | ||
const wchar_t *haystack = L"abcdefghi"; | ||
const wchar_t *needle = L"ghi"; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 6); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, NeedlePastNullTerminator) { | ||
const wchar_t haystack[5] = {L'1', L'2', L'\0', L'3', L'4'}; | ||
// Shouldn't find anything after the null terminator. | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, /*needle=*/L"3"), nullptr); | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, /*needle=*/L"4"), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, PartialNeedle) { | ||
const wchar_t *haystack = L"la_ap_lap"; | ||
const wchar_t *needle = L"lap"; | ||
// Shouldn't find la or ap. | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 6); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, MisspelledNeedle) { | ||
const wchar_t *haystack = L"atalloftwocities...wait, tale"; | ||
const wchar_t *needle = L"tale"; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 25); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, AnagramNeedle) { | ||
const wchar_t *haystack = L"dgo_ogd_god_odg_gdo_dog"; | ||
const wchar_t *needle = L"dog"; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, needle), haystack + 20); | ||
} | ||
|
||
TEST(LlvmLibcWCSStrTest, MorphedNeedle) { | ||
// Changes a single letter in the needle to mismatch with the haystack. | ||
const wchar_t *haystack = L"once upon a time"; | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"time"), haystack + 12); | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"lime"), nullptr); | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"tome"), nullptr); | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"tire"), nullptr); | ||
ASSERT_EQ(LIBC_NAMESPACE::wcsstr(haystack, L"timo"), nullptr); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.