Skip to content

Commit f6f79eb

Browse files
committed
feat: add str::precompose_bstr() for convenience
1 parent 4a18be5 commit f6f79eb

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-utils/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ include = ["src/**/*", "LICENSE-*"]
1212
[lib]
1313
doctest = false
1414

15+
[features]
16+
bstr = ["dep:bstr"]
17+
1518
[dependencies]
1619
fastrand = "2.0.0"
20+
bstr = { version = "1.5.0", default-features = false, features = ["std"], optional = true }
1721
unicode-normalization = { version = "0.1.19", default-features = false }
1822

gix-utils/src/str.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,26 @@ pub fn precompose_path(path: Cow<'_, Path>) -> Cow<'_, Path> {
4242
/// Return the precomposed version of `name`, or `name` itself if it contained illformed unicode,
4343
/// or if the unicode version didn't contains decomposed unicode.
4444
/// Otherwise, similar to [`precompose()`]
45-
pub fn precompose_os_string(path: Cow<'_, OsStr>) -> Cow<'_, OsStr> {
46-
match path.to_str() {
47-
None => path,
45+
pub fn precompose_os_string(name: Cow<'_, OsStr>) -> Cow<'_, OsStr> {
46+
match name.to_str() {
47+
None => name,
4848
Some(maybe_decomposed) => match precompose(maybe_decomposed.into()) {
49-
Cow::Borrowed(_) => path,
49+
Cow::Borrowed(_) => name,
50+
Cow::Owned(precomposed) => Cow::Owned(precomposed.into()),
51+
},
52+
}
53+
}
54+
55+
/// Return the precomposed version of `s`, or `s` itself if it contained illformed unicode,
56+
/// or if the unicode version didn't contains decomposed unicode.
57+
/// Otherwise, similar to [`precompose()`]
58+
#[cfg(feature = "bstr")]
59+
pub fn precompose_bstr(s: Cow<'_, bstr::BStr>) -> Cow<'_, bstr::BStr> {
60+
use bstr::ByteSlice;
61+
match s.to_str().ok() {
62+
None => s,
63+
Some(maybe_decomposed) => match precompose(maybe_decomposed.into()) {
64+
Cow::Borrowed(_) => s,
5065
Cow::Owned(precomposed) => Cow::Owned(precomposed.into()),
5166
},
5267
}

0 commit comments

Comments
 (0)