Skip to content

Commit 674c9e4

Browse files
committed
Merge branch 'master' into publish-3781
2 parents 2b887f3 + 5cd6d3f commit 674c9e4

File tree

8,281 files changed

+276141
-36013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

8,281 files changed

+276141
-36013
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Security.Cryptography;
4+
using System.Text;
5+
6+
namespace RuneSamples
7+
{
8+
public static class CountLettersInSpan
9+
{
10+
public static void Run()
11+
{
12+
Console.WriteLine($"Incorrect code: { CountLettersBadExample(new Span<char>("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟".ToCharArray()))} letters in 𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");
13+
Console.WriteLine($" Correct code: { CountLetters(new Span<char>("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟".ToCharArray()))} letters in 𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");
14+
15+
// <SnippetBadExample>
16+
// THE FOLLOWING METHOD SHOWS INCORRECT CODE.
17+
// DO NOT DO THIS IN A PRODUCTION APPLICATION.
18+
static int CountLettersBadExample(ReadOnlySpan<char> span)
19+
{
20+
int letterCount = 0;
21+
22+
foreach (char ch in span)
23+
{
24+
if (char.IsLetter(ch))
25+
{ letterCount++; }
26+
}
27+
28+
return letterCount;
29+
}
30+
// </SnippetBadExample>
31+
32+
// <SnippetGoodExample>
33+
static int CountLetters(ReadOnlySpan<char> span)
34+
{
35+
int letterCount = 0;
36+
37+
foreach (Rune rune in span.EnumerateRunes())
38+
{
39+
if (Rune.IsLetter(rune))
40+
{ letterCount++; }
41+
}
42+
43+
return letterCount;
44+
}
45+
// </SnippetGoodExample>
46+
}
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace RuneSamples
6+
{
7+
public static class CountLettersInString
8+
{
9+
public static void Run()
10+
{
11+
Console.WriteLine($"Incorrect code: { CountLettersBadExample("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟")} letters in 𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");
12+
Console.WriteLine($" Correct code: { CountLetters("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟")} letters in 𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");
13+
//Console.WriteLine($"String '𐓏𐓘𐓻𐓘𐓻𐓟𐒻𐓟' consists entirely of letters: { StringConsistsEntirelyOfLetters("𐓏𐓘𐓻𐓘𐓻𐓟𐒻𐓟")}");
14+
15+
// <SnippetBadExample>
16+
// THE FOLLOWING METHOD SHOWS INCORRECT CODE.
17+
// DO NOT DO THIS IN A PRODUCTION APPLICATION.
18+
int CountLettersBadExample(string s)
19+
{
20+
int letterCount = 0;
21+
22+
foreach (char ch in s)
23+
{
24+
if (char.IsLetter(ch))
25+
{ letterCount++; }
26+
}
27+
28+
return letterCount;
29+
}
30+
// </SnippetBadExample>
31+
32+
// <SnippetGoodExample>
33+
int CountLetters(string s)
34+
{
35+
int letterCount = 0;
36+
37+
foreach (Rune rune in s.EnumerateRunes())
38+
{
39+
if (Rune.IsLetter(rune))
40+
{ letterCount++; }
41+
}
42+
43+
return letterCount;
44+
}
45+
// </SnippetGoodExample>
46+
}
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace RuneSamples
6+
{
7+
public static class EncodeRune
8+
{
9+
public static void Run()
10+
{
11+
Console.WriteLine("Converting ox emoji to UTF-16 and UTF-8");
12+
Rune rune = Rune.GetRuneAt("🐂", 0);
13+
Console.WriteLine($"Rune value: {rune.Value}");
14+
15+
// <SnippetUtf16CharArray>
16+
char[] chars = new char[rune.Utf16SequenceLength];
17+
int numCharsWritten = rune.EncodeToUtf16(chars);
18+
// </SnippetUtf16CharArray>
19+
20+
Console.WriteLine($"Number of chars: {numCharsWritten}");
21+
22+
// <SnippetUtf16String>
23+
string theString = rune.ToString();
24+
// </SnippetUtf16String>
25+
26+
// <SnippetUtf8ByteArray>
27+
byte[] bytes = new byte[rune.Utf8SequenceLength];
28+
int numBytesWritten = rune.EncodeToUtf8(bytes);
29+
// </SnippetUtf8ByteArray>
30+
31+
Console.WriteLine($"Number of UTF-8 bytes: {numBytesWritten}");
32+
}
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace RuneSamples
6+
{
7+
public static class FindFirstLetter
8+
{
9+
public static void Run()
10+
{
11+
Console.WriteLine($"Index of first capital letter of Latin alphabet in \"𐓏𐓘𐓻𐓘𐓻𐓟F𐒻𐓟\": { GetIndexOfFirstAToZ("𐓏𐓘𐓻𐓘𐓻𐓟F𐒻𐓟")}");
12+
13+
// <SnippetExample>
14+
int GetIndexOfFirstAToZ(string s)
15+
{
16+
for (int i = 0; i < s.Length; i++)
17+
{
18+
char thisChar = s[i];
19+
if ('A' <= thisChar && thisChar <= 'Z')
20+
{
21+
return i; // found a match
22+
}
23+
}
24+
25+
return -1; // didn't find 'A' - 'Z' in the input string
26+
}
27+
// </SnippetExample>
28+
}
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace RuneSamples
6+
{
7+
public class InstantiateRunes
8+
{
9+
public static void Run()
10+
{
11+
// <SnippetCodePoint>
12+
Rune a = new Rune(0x0061); // LATIN SMALL LETTER A
13+
Rune b = new Rune(0x10421); // DESERET CAPITAL LETTER ER
14+
// </SnippetCodePoint>
15+
16+
// <SnippetChar>
17+
Rune c = new Rune('a');
18+
// </SnippetChar>
19+
20+
// <SnippetSurrogate>
21+
Rune d = new Rune('\ud83d', '\udd2e'); // U+1F52E CRYSTAL BALL
22+
// </SnippetSurrogate>
23+
24+
Console.WriteLine($"Rune a: {a}");
25+
Console.WriteLine($"Rune b: {b}");
26+
Console.WriteLine($"Rune c: {c}");
27+
Console.WriteLine($"Rune d: {d}");
28+
Console.WriteLine($"Rune d code point: {d.Value}");
29+
30+
// <SnippetValue>
31+
Rune rune = new Rune('\ud83d', '\udd2e'); // U+1F52E CRYSTAL BALL
32+
int codePoint = rune.Value; // = 128302 decimal (= 0x1F52E)
33+
// </SnippetValue>
34+
35+
}
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Text;
4+
using System.Linq;
5+
using System.Globalization;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace RuneSamples
9+
{
10+
partial class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
Console.WriteLine("\n----- Count letters in string");
15+
CountLettersInString.Run();
16+
17+
Console.WriteLine("\n----- Count letters in span");
18+
CountLettersInSpan.Run();
19+
20+
Console.WriteLine("\n----- Handle surrogates");
21+
WorkWithSurrogates.Run();
22+
23+
Console.WriteLine("\n----- Find first letter");
24+
FindFirstLetter.Run();
25+
26+
Console.WriteLine("\n----- Split string on char");
27+
SplitStringOnChar.Run();
28+
29+
Console.WriteLine("\n----- Instantiate Runes");
30+
InstantiateRunes.Run();
31+
32+
Console.WriteLine("\n----- Trim letters and nondigits");
33+
TrimNonLettersAndNonDigits.Run();
34+
35+
Console.WriteLine("\n----- Encode a Rune");
36+
EncodeRune.Run();
37+
}
38+
}
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace RuneSamples
6+
{
7+
public static class SplitStringOnChar
8+
{
9+
public static void Run()
10+
{
11+
// <SnippetExample>
12+
string inputString = "🐂, 🐄, 🐆";
13+
string[] splitOnSpace = inputString.Split(' ');
14+
string[] splitOnComma = inputString.Split(',');
15+
// </SnippetExample>
16+
17+
Console.WriteLine($"Splitting string {inputString} on space");
18+
Array.ForEach(splitOnSpace, s => Console.WriteLine(s));
19+
20+
Console.WriteLine($"Splitting string {inputString} on comma");
21+
Array.ForEach(splitOnComma, s => Console.WriteLine(s));
22+
}
23+
}
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace RuneSamples
7+
{
8+
class TrimNonLettersAndNonDigits
9+
{
10+
public static void Run()
11+
{
12+
char[] s = "!Hello!".ToCharArray();
13+
ReadOnlySpan<char> span = new ReadOnlySpan<char>(s);
14+
Console.WriteLine(span.ToString());
15+
ReadOnlySpan<char> newSpan = TrimNonLettersAndNonDigits(span);
16+
Console.WriteLine(newSpan.ToString());
17+
18+
// <SnippetExample>
19+
static ReadOnlySpan<char> TrimNonLettersAndNonDigits(ReadOnlySpan<char> span)
20+
{
21+
// First, trim from the front.
22+
// If any Rune can't be decoded
23+
// (return value is anything other than "Done"),
24+
// or if the Rune is a letter or digit,
25+
// stop trimming from the front and
26+
// instead work from the end.
27+
while (Rune.DecodeFromUtf16(span, out Rune rune, out int charsConsumed) == OperationStatus.Done)
28+
{
29+
if (Rune.IsLetterOrDigit(rune))
30+
{ break; }
31+
span = span[charsConsumed..];
32+
}
33+
34+
// Next, trim from the end.
35+
// If any Rune can't be decoded,
36+
// or if the Rune is a letter or digit,
37+
// break from the loop, and we're finished.
38+
while (Rune.DecodeLastFromUtf16(span, out Rune rune, out int charsConsumed) == OperationStatus.Done)
39+
{
40+
if (Rune.IsLetterOrDigit(rune))
41+
{ break; }
42+
span = span[..^charsConsumed];
43+
}
44+
45+
return span;
46+
}
47+
// <SnippetExample>
48+
}
49+
}
50+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace RuneSamples
6+
{
7+
public static class WorkWithSurrogates
8+
{
9+
public static void Run()
10+
{
11+
ProcessStringUseChar("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");
12+
ProcessStringUseRune("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");
13+
14+
// <SnippetUseChar>
15+
static void ProcessStringUseChar(string s)
16+
{
17+
Console.WriteLine("Using char");
18+
19+
for (int i = 0; i < s.Length; i++)
20+
{
21+
if (!char.IsSurrogate(s[i]))
22+
{
23+
Console.WriteLine($"Code point: {(int)(s[i])}");
24+
}
25+
else if (i + 1 < s.Length && char.IsSurrogatePair(s[i], s[i + 1]))
26+
{
27+
int codePoint = char.ConvertToUtf32(s[i], s[i + 1]);
28+
Console.WriteLine($"Code point: {codePoint}");
29+
i++; // so that when the loop iterates it's actually +2
30+
}
31+
else
32+
{
33+
throw new Exception("String was not well-formed UTF-16.");
34+
}
35+
}
36+
}
37+
// </SnippetUseChar>
38+
39+
// <SnippetUseRune>
40+
static void ProcessStringUseRune(string s)
41+
{
42+
Console.WriteLine("Using Rune");
43+
44+
for (int i = 0; i < s.Length;)
45+
{
46+
if (!Rune.TryGetRuneAt(s, i, out Rune rune))
47+
{
48+
throw new Exception("String was not well-formed UTF-16.");
49+
}
50+
51+
Console.WriteLine($"Code point: {rune.Value}");
52+
i += rune.Utf16SequenceLength; // increment the iterator by the number of chars in this Rune
53+
}
54+
}
55+
// </SnippetUseRune>
56+
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)