diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-10-17 15:42:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-17 15:42:47 +0200 |
commit | 5b6a22e275ef8d8e2ad3b1e8074af4b30c076c27 (patch) | |
tree | 96063e286f618c07b0247da5215625730789ba0b | |
parent | 94e5f66cbb399ffd3e501a237d7eb3de79ce1607 (diff) | |
parent | 029de5200135e68cd6cd2727fa110fa50100849d (diff) |
Merge pull request #42867 from aaronfranke/cs_get_str_bytes
Add GetStringFromUTF8 and GetStringFromASCII to C#
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index bd1dbc1229..d63db0f905 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -393,6 +393,35 @@ namespace Godot return instance.Substring(sep + 1); } + /// <summary> + /// Converts the given byte array of ASCII encoded text to a string. + /// Faster alternative to <see cref="GetStringFromUTF8"/> if the + /// content is ASCII-only. Unlike the UTF-8 function this function + /// maps every byte to a character in the array. Multibyte sequences + /// will not be interpreted correctly. For parsing user input always + /// use <see cref="GetStringFromUTF8"/>. + /// </summary> + /// <param name="bytes">A byte array of ASCII characters (on the range of 0-127).</param> + /// <returns>A string created from the bytes.</returns> + public static string GetStringFromASCII(this byte[] bytes) + { + return Encoding.ASCII.GetString(bytes); + } + + /// <summary> + /// Converts the given byte array of UTF-8 encoded text to a string. + /// Slower than <see cref="GetStringFromASCII"/> but supports UTF-8 + /// encoded data. Use this function if you are unsure about the + /// source of the data. For user input this function + /// should always be preferred. + /// </summary> + /// <param name="bytes">A byte array of UTF-8 characters (a character may take up multiple bytes).</param> + /// <returns>A string created from the bytes.</returns> + public static string GetStringFromUTF8(this byte[] bytes) + { + return Encoding.UTF8.GetString(bytes); + } + // <summary> // Hash the string and return a 32 bits integer. // </summary> |