From 776d102baaff226fcc60d90e2298ffeb736baa0b Mon Sep 17 00:00:00 2001 From: Alex Hamilton Date: Thu, 25 Mar 2021 18:09:36 -0400 Subject: [PATCH 1/4] Add char::code_point This method is equivalent to conversion using `as`, but with a more domain-specific name. --- library/core/src/char/methods.rs | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 87a3d375a6951..c94c272b46dc1 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -673,6 +673,39 @@ impl char { encode_utf16_raw(self as u32, dst) } + /// Returns the ["Unicode code point"][codepoint] associated with this `char`. + /// + /// Although ["Unicode scalar values"][scalar] and ["code points"][codepoint] are different, + /// all scalar values are code points. Because a `char` is a scalar value, it therefore is also + /// a valid code point. This method provides that code point as a `u32`. + /// + /// [scalar]: http://www.unicode.org/glossary/#unicode_scalar_value + /// [codepoint]: http://www.unicode.org/glossary/#code_point + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// assert_eq!('💯'.code_point(), 65); + /// assert_eq!('❤'.code_point(), 0x2764); + /// assert_eq!('a'.code_point(), 97); + /// ``` + /// + /// This method is equivalent to casting via `as`, but with a more descriptive name: + /// + /// ``` + /// assert_eq!('a'.code_point(), 'a' as u32); + /// assert_eq!('ß'.code_point(), 'ß' as u32); + /// assert_eq!('\n'.code_point(), '\n' as u32); + /// ``` + #[inline] + pub const fn code_point(self) -> u32 { + // Casting a `char` to a `u32` gives the underlying scalar value, and all scalar values are + // valid code points + self as u32 + } + /// Returns `true` if this `char` has the `Alphabetic` property. /// /// `Alphabetic` is described in Chapter 4 (Character Properties) of the [Unicode Standard] and From 41b85d74a0b4c8338079c87fa59b29594cd9900a Mon Sep 17 00:00:00 2001 From: Alex Hamilton Date: Thu, 25 Mar 2021 21:20:22 -0400 Subject: [PATCH 2/4] Add stability annotation for char::code_point --- library/core/src/char/methods.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index c94c272b46dc1..104fd0f98f05d 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -699,6 +699,7 @@ impl char { /// assert_eq!('ß'.code_point(), 'ß' as u32); /// assert_eq!('\n'.code_point(), '\n' as u32); /// ``` + #[stable(feature = "char_code_point", since = "1.53.0")] #[inline] pub const fn code_point(self) -> u32 { // Casting a `char` to a `u32` gives the underlying scalar value, and all scalar values are From a488009dfe936270768f29c38afa6f72d9acf3b1 Mon Sep 17 00:00:00 2001 From: Alex Hamilton Date: Thu, 25 Mar 2021 21:35:43 -0400 Subject: [PATCH 3/4] Add const stability annotation to char::code_point --- library/core/src/char/methods.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 104fd0f98f05d..c13dc3105a2ae 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -700,6 +700,7 @@ impl char { /// assert_eq!('\n'.code_point(), '\n' as u32); /// ``` #[stable(feature = "char_code_point", since = "1.53.0")] + #[rustc_const_stable(feature = "char_code_point", since = "1.53.0")] #[inline] pub const fn code_point(self) -> u32 { // Casting a `char` to a `u32` gives the underlying scalar value, and all scalar values are From 11ee4bf274c0c99f061f2c9d531b14980eab286d Mon Sep 17 00:00:00 2001 From: Alex Hamilton Date: Thu, 25 Mar 2021 22:04:10 -0400 Subject: [PATCH 4/4] Fix incorrect value in char::code_point doctest The example was previously based on the character "a," but was since revised to use a more "interesting" character. When making this change, I neglected to change the "magic number" in the doctest. On the upside, I think the tests should pass now. --- library/core/src/char/methods.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index c13dc3105a2ae..c99f5b2a9c984 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -687,7 +687,7 @@ impl char { /// Basic usage: /// /// ``` - /// assert_eq!('💯'.code_point(), 65); + /// assert_eq!('💯'.code_point(), 0x1F4AF); /// assert_eq!('❤'.code_point(), 0x2764); /// assert_eq!('a'.code_point(), 97); /// ```