Skip to content

Commit 37cf025

Browse files
committed
Auto merge of #26154 - pmarcelll:master, r=Gankro
Various methods in both libcore/char.rs and librustc_unicode/char.rs were previously marked with #[inline], now every method is marked in char's impl blocks. Partially fixes #26124. EDIT: I'm not familiar with pull reqests (yet), apparently Github added my second commit to thit PR... Fixes #26124
2 parents 2fbbd54 + e87c62f commit 37cf025

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

src/libcollections/string.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -468,24 +468,24 @@ impl String {
468468
#[inline]
469469
#[stable(feature = "rust1", since = "1.0.0")]
470470
pub fn push(&mut self, ch: char) {
471-
if (ch as u32) < 0x80 {
472-
self.vec.push(ch as u8);
473-
return;
474-
}
475-
476-
let cur_len = self.len();
477-
// This may use up to 4 bytes.
478-
self.vec.reserve(4);
471+
match ch.len_utf8() {
472+
1 => self.vec.push(ch as u8),
473+
ch_len => {
474+
let cur_len = self.len();
475+
// This may use up to 4 bytes.
476+
self.vec.reserve(ch_len);
479477

480-
unsafe {
481-
// Attempt to not use an intermediate buffer by just pushing bytes
482-
// directly onto this string.
483-
let slice = slice::from_raw_parts_mut (
484-
self.vec.as_mut_ptr().offset(cur_len as isize),
485-
4
486-
);
487-
let used = ch.encode_utf8(slice).unwrap_or(0);
488-
self.vec.set_len(cur_len + used);
478+
unsafe {
479+
// Attempt to not use an intermediate buffer by just pushing bytes
480+
// directly onto this string.
481+
let slice = slice::from_raw_parts_mut (
482+
self.vec.as_mut_ptr().offset(cur_len as isize),
483+
ch_len
484+
);
485+
let used = ch.encode_utf8(slice).unwrap_or(0);
486+
self.vec.set_len(cur_len + used);
487+
}
488+
}
489489
}
490490
}
491491

src/libcore/char.rs

+4
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ pub trait CharExt {
152152
}
153153

154154
impl CharExt for char {
155+
#[inline]
155156
fn is_digit(self, radix: u32) -> bool {
156157
self.to_digit(radix).is_some()
157158
}
158159

160+
#[inline]
159161
fn to_digit(self, radix: u32) -> Option<u32> {
160162
if radix > 36 {
161163
panic!("to_digit: radix is too high (maximum 36)");
@@ -170,10 +172,12 @@ impl CharExt for char {
170172
else { None }
171173
}
172174

175+
#[inline]
173176
fn escape_unicode(self) -> EscapeUnicode {
174177
EscapeUnicode { c: self, state: EscapeUnicodeState::Backslash }
175178
}
176179

180+
#[inline]
177181
fn escape_default(self) -> EscapeDefault {
178182
let init_state = match self {
179183
'\t' => EscapeDefaultState::Backslash('t'),

src/librustc_unicode/char.rs

+8
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ impl char {
174174
/// assert_eq!('f'.to_digit(16), Some(15));
175175
/// ```
176176
#[stable(feature = "rust1", since = "1.0.0")]
177+
#[inline]
177178
pub fn to_digit(self, radix: u32) -> Option<u32> { C::to_digit(self, radix) }
178179

179180
/// Returns an iterator that yields the hexadecimal Unicode escape of a
@@ -212,6 +213,7 @@ impl char {
212213
/// assert_eq!(heart, r"\u{2764}");
213214
/// ```
214215
#[stable(feature = "rust1", since = "1.0.0")]
216+
#[inline]
215217
pub fn escape_unicode(self) -> EscapeUnicode { C::escape_unicode(self) }
216218

217219
/// Returns an iterator that yields the 'default' ASCII and
@@ -250,6 +252,7 @@ impl char {
250252
/// assert_eq!(quote, "\\\"");
251253
/// ```
252254
#[stable(feature = "rust1", since = "1.0.0")]
255+
#[inline]
253256
pub fn escape_default(self) -> EscapeDefault { C::escape_default(self) }
254257

255258
/// Returns the number of bytes this character would need if encoded in
@@ -263,6 +266,7 @@ impl char {
263266
/// assert_eq!(n, 2);
264267
/// ```
265268
#[stable(feature = "rust1", since = "1.0.0")]
269+
#[inline]
266270
pub fn len_utf8(self) -> usize { C::len_utf8(self) }
267271

268272
/// Returns the number of 16-bit code units this character would need if
@@ -276,6 +280,7 @@ impl char {
276280
/// assert_eq!(n, 1);
277281
/// ```
278282
#[stable(feature = "rust1", since = "1.0.0")]
283+
#[inline]
279284
pub fn len_utf16(self) -> usize { C::len_utf16(self) }
280285

281286
/// Encodes this character as UTF-8 into the provided byte buffer, and then
@@ -310,6 +315,7 @@ impl char {
310315
/// ```
311316
#[unstable(feature = "unicode",
312317
reason = "pending decision about Iterator/Writer/Reader")]
318+
#[inline]
313319
pub fn encode_utf8(self, dst: &mut [u8]) -> Option<usize> { C::encode_utf8(self, dst) }
314320

315321
/// Encodes this character as UTF-16 into the provided `u16` buffer, and
@@ -344,6 +350,7 @@ impl char {
344350
/// ```
345351
#[unstable(feature = "unicode",
346352
reason = "pending decision about Iterator/Writer/Reader")]
353+
#[inline]
347354
pub fn encode_utf16(self, dst: &mut [u16]) -> Option<usize> { C::encode_utf16(self, dst) }
348355

349356
/// Returns whether the specified character is considered a Unicode
@@ -527,5 +534,6 @@ impl char {
527534
since = "1.0.0")]
528535
#[unstable(feature = "unicode",
529536
reason = "needs expert opinion. is_cjk flag stands out as ugly")]
537+
#[inline]
530538
pub fn width(self, is_cjk: bool) -> Option<usize> { charwidth::width(self, is_cjk) }
531539
}

0 commit comments

Comments
 (0)