-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update dependencies and make use of modern idioms (#16)
- Loading branch information
Showing
8 changed files
with
175 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: nuget | ||
directory: "/" | ||
directory: '/' | ||
schedule: | ||
interval: monthly | ||
time: "10:00" | ||
interval: weekly | ||
time: '10:00' | ||
open-pull-requests-limit: 10 | ||
# ignore: | ||
# - dependency-name: Microsoft.CodeAnalysis.FxCopAnalyzers | ||
# versions: | ||
# - 3.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,51 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace IntroToSpans | ||
namespace IntroToSpans; | ||
|
||
/// <summary> | ||
/// Getting familiar with Span<T>... | ||
/// </summary> | ||
/// <see href="https://github.com/dotnet/corefxlab/blob/master/docs/specs/span.md"/> | ||
/// <see href="https://docs.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay"/> | ||
internal class Program | ||
{ | ||
/// <summary> | ||
/// Getting familiar with Span<T>... | ||
/// </summary> | ||
/// <see href="https://github.com/dotnet/corefxlab/blob/master/docs/specs/span.md"/> | ||
/// <see href="https://docs.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay"/> | ||
internal class Program | ||
private static void Main() | ||
{ | ||
private static void Main() | ||
{ | ||
TestSpanWithAllMemoryTypes(); | ||
} | ||
TestSpanWithAllMemoryTypes(); | ||
} | ||
|
||
private static void TestSpanWithAllMemoryTypes() | ||
{ | ||
// managed memory | ||
var byteArray = new byte[100]; | ||
Span<byte> byteSpanOnManagedMemory = byteArray; | ||
|
||
private static void TestSpanWithAllMemoryTypes() | ||
// native memory | ||
var nativeMemory = Marshal.AllocHGlobal(100); | ||
Span<byte> byteSpanOnNativeMemory; | ||
unsafe | ||
{ | ||
// managed memory | ||
var byteArray = new byte[100]; | ||
Span<byte> byteSpanOnManagedMemory = byteArray; | ||
|
||
// native memory | ||
var nativeMemory = Marshal.AllocHGlobal(100); | ||
Span<byte> byteSpanOnNativeMemory; | ||
unsafe | ||
{ | ||
byteSpanOnNativeMemory = new Span<byte>(nativeMemory.ToPointer(), 100); | ||
} | ||
|
||
SafeSum(byteSpanOnNativeMemory); | ||
Marshal.FreeHGlobal(nativeMemory); | ||
|
||
// stack memory | ||
Span<byte> byteSpanOnStackMemory = stackalloc byte[100]; | ||
SafeSum(byteSpanOnStackMemory); | ||
byteSpanOnNativeMemory = new Span<byte>(nativeMemory.ToPointer(), 100); | ||
} | ||
|
||
// this method does not care what kind of memory it works on | ||
private static ulong SafeSum(Span<byte> bytes) | ||
{ | ||
ulong sum = 0; | ||
for (var i = 0; i < bytes.Length; i++) | ||
{ | ||
sum += bytes[i]; | ||
} | ||
SafeSum(byteSpanOnNativeMemory); | ||
Marshal.FreeHGlobal(nativeMemory); | ||
|
||
// stack memory | ||
Span<byte> byteSpanOnStackMemory = stackalloc byte[100]; | ||
SafeSum(byteSpanOnStackMemory); | ||
} | ||
|
||
return sum; | ||
// this method does not care what kind of memory it works on | ||
private static ulong SafeSum(Span<byte> bytes) | ||
{ | ||
ulong sum = 0; | ||
for (var i = 0; i < bytes.Length; i++) | ||
{ | ||
sum += bytes[i]; | ||
} | ||
|
||
return sum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
using System; | ||
|
||
namespace TestingSpan | ||
namespace TestingSpan; | ||
|
||
public static class StringExtensions | ||
{ | ||
public static class StringExtensions | ||
{ | ||
public static readonly char[] WordSeparators = new[] { ' ', '\r', '\n', '\t' }; | ||
public static readonly char[] WordSeparators = [' ', '\r', '\n', '\t']; | ||
|
||
public static WordEnumerator SplitIntoWords(this string str) | ||
{ | ||
return new WordEnumerator(str.AsSpan()); // WordEnumerator is a struct -> no heap allocation here | ||
} | ||
public static WordEnumerator SplitIntoWords(this string str) | ||
{ | ||
return new WordEnumerator(str.AsSpan()); // WordEnumerator is a struct -> no heap allocation here | ||
} | ||
} |
Oops, something went wrong.