Skip to content

Commit 06f455c

Browse files
committed
Add NSString::concat and NSString::join_path
1 parent 41e8f84 commit 06f455c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

objc2/CHANGELOG_FOUNDATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1616
* Added `NSBundle`.
1717
* Added `NSTimeInterval`.
1818
* Added `NSString::len_utf16` and `NSAttributedString::len_utf16`.
19+
* Added `NSString::concat` and `NSString::join_path`.
1920

2021

2122
## objc2 0.3.0-beta.2 - 2022-08-28

objc2/src/foundation/string.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,57 @@ extern_methods!(
5656
unsafe { msg_send_id![Self::class(), new] }
5757
}
5858

59+
/// Create a new string by appending the given string to self.
60+
///
61+
///
62+
/// # Example
63+
///
64+
/// ```
65+
/// # #[cfg(feature = "gnustep-1-7")]
66+
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
67+
/// use objc2::ns_string;
68+
/// let error_tag = ns_string!("Error: ");
69+
/// let error_string = ns_string!("premature end of file.");
70+
/// let error_message = error_tag.concat(error_string);
71+
/// assert_eq!(&*error_message, ns_string!("Error: premature end of file."));
72+
/// ```
73+
#[doc(alias = "stringByAppendingString")]
74+
#[doc(alias = "stringByAppendingString:")]
75+
pub fn concat(&self, other: &Self) -> Id<Self, Shared> {
76+
// SAFETY: The other string is non-null, and won't be retained
77+
// by the function.
78+
unsafe { msg_send_id![self, stringByAppendingString: other] }
79+
}
80+
81+
/// Create a new string by appending the given string, separated by
82+
/// a path separator.
83+
///
84+
/// This is similar to [`Path::join`][std::path::Path::join].
85+
///
86+
/// Note that this method only works with file paths (not, for
87+
/// example, string representations of URLs).
88+
///
89+
///
90+
/// # Examples
91+
///
92+
/// ```
93+
/// # #[cfg(feature = "gnustep-1-7")]
94+
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
95+
/// use objc2::ns_string;
96+
///
97+
/// let extension = ns_string!("scratch.tiff");
98+
/// assert_eq!(&*ns_string!("/tmp").join_path(extension), ns_string!("/tmp/scratch.tiff"));
99+
/// assert_eq!(&*ns_string!("/tmp/").join_path(extension), ns_string!("/tmp/scratch.tiff"));
100+
/// assert_eq!(&*ns_string!("/").join_path(extension), ns_string!("/scratch.tiff"));
101+
/// assert_eq!(&*ns_string!("").join_path(extension), ns_string!("scratch.tiff"));
102+
/// ```
103+
#[doc(alias = "stringByAppendingPathComponent")]
104+
#[doc(alias = "stringByAppendingPathComponent:")]
105+
pub fn join_path(&self, other: &Self) -> Id<Self, Shared> {
106+
// SAFETY: Same as `Self::concat`.
107+
unsafe { msg_send_id![self, stringByAppendingPathComponent: other] }
108+
}
109+
59110
/// The number of UTF-8 code units in `self`.
60111
#[doc(alias = "lengthOfBytesUsingEncoding")]
61112
#[doc(alias = "lengthOfBytesUsingEncoding:")]

0 commit comments

Comments
 (0)