-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[libc] Cleaned up wcsspn and wcscspn #147408
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===-- wchar utils ---------------------------------------------*- 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_WCHAR_WCHAR_UTILS_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H | ||
|
||
#include "hdr/types/size_t.h" | ||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/attributes.h" // LIBC_INLINE | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
|
||
// returns true if the character exists in the string | ||
LIBC_INLINE bool internal_wcschr(wchar_t c, const wchar_t *str) { | ||
for (int n = 0; str[n]; ++n) { | ||
if (str[n] == c) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// To avoid duplicated code, call this with true for wcscspn and call with false | ||
// for wcsspn | ||
sribee8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LIBC_INLINE size_t inline_wcsspn(const wchar_t *s1, const wchar_t *s2, | ||
bool invert) { | ||
size_t i = 0; | ||
for (; s1[i]; ++i) { | ||
bool check = internal_wcschr(s1[i], s2); | ||
check = invert ? !check : check; | ||
if (!check) | ||
return i; | ||
sribee8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return i; | ||
} | ||
|
||
} // namespace internal | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,23 +12,12 @@ | |
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "wchar_utils.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
bool check(wchar_t c, const wchar_t *s2) { | ||
for (int n = 0; s2[n]; ++n) { | ||
if (s2[n] == c) | ||
return false; | ||
} | ||
return true; | ||
} | ||
LLVM_LIBC_FUNCTION(size_t, wcscspn, (const wchar_t *s1, const wchar_t *s2)) { | ||
size_t i = 0; | ||
for (; s1[i]; ++i) { | ||
if (!check(s1[i], s2)) | ||
return i; | ||
} | ||
return i; | ||
return internal::inline_wcsspn(s1, s2, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are here, can you also add |
||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,23 +12,12 @@ | |
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "wchar_utils.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
bool check(wchar_t c, const wchar_t *s2) { | ||
for (int n = 0; s2[n]; ++n) { | ||
if (s2[n] == c) | ||
return true; | ||
} | ||
return false; | ||
} | ||
LLVM_LIBC_FUNCTION(size_t, wcsspn, (const wchar_t *s1, const wchar_t *s2)) { | ||
size_t i = 0; | ||
for (; s1[i]; ++i) { | ||
if (!check(s1[i], s2)) | ||
return i; | ||
} | ||
return i; | ||
return internal::inline_wcsspn(s1, s2, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably also mark these 2
static
. We don't need to export their symbols anyway. Also, since they are already in theinternal
namespace, just keep their names towcschr
andwcsspn
?