Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Commit 88b4a06

Browse files
authored
Merge pull request #55 from InitialPrefabs/develop
Develop (0.3.0 - NumericUtils)
2 parents 4d8ee21 + 4d39b46 commit 88b4a06

17 files changed

+275
-10
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Change Log
22

3+
## 0.3.0
4+
### Added
5+
* NumericsUtils to convert integers to character arrays.
6+
7+
### Changed
8+
* Updates the minimum dependency to Entities 0.11.0
9+
10+
### Removed
11+
* Removes checks of a null EntityManager in the test fixture.
12+
313
## 0.2.0
414
### Added
515
* Added `ChildrenActiveMetadata` component to store the state of the visibility of the entity

Common.Tests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Common.Tests/NumericUtilsTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using NUnit.Framework;
2+
3+
namespace CarteDiem.Common.Tests {
4+
5+
public unsafe class NumericUtilTests {
6+
7+
[Test]
8+
public void CharEqualsPositiveDigit() {
9+
int value = 1234567890;
10+
11+
char* actual = stackalloc char[10];
12+
13+
value.ToCharArray(actual, 10, out int count);
14+
15+
string expected = value.ToString();
16+
17+
for (int i = 0; i < count; i++) {
18+
Assert.AreEqual(expected[i], actual[i], "Mismatched digits");
19+
}
20+
}
21+
22+
[Test]
23+
public void CharEqualsPositiveDigitWithExcess() {
24+
int value = 12345;
25+
26+
char* actual = stackalloc char[15];
27+
value.ToCharArray(actual, 15, out int count);
28+
string exepected = value.ToString();
29+
30+
for (int i = 0; i < count; i++) {
31+
32+
Assert.AreEqual(exepected[i], actual[i], "Mismatched digits");
33+
}
34+
}
35+
36+
[Test]
37+
public void CharEqualsNegativeDigits() {
38+
int value = -12345;
39+
40+
char* actual = stackalloc char[6];
41+
value.ToCharArray(actual, 6, out int count);
42+
string expected = value.ToString();
43+
44+
for (int i = 0; i < count; i++) {
45+
Assert.AreEqual(expected[i], actual[i], "Mismatch digits");
46+
}
47+
}
48+
49+
[Test]
50+
public void CharEqualsNegativeDigitsWIthExcess() {
51+
int value = -1234569;
52+
53+
char* actual = stackalloc char[15];
54+
value.ToCharArray(actual, 15, out int count);
55+
string expected = value.ToString();
56+
57+
for (int i = 0; i < count; i++) {
58+
Assert.AreEqual(expected[i], actual[i], "Mismatch digits");
59+
}
60+
}
61+
}
62+
}

Common.Tests/NumericUtilsTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "UGUIDots.Common.Tests",
3+
"references": [
4+
"GUID:5f36bc0aeb435984fb19ecbd27b87a1f",
5+
"GUID:e0cd26848372d4e5c891c569017e11f1",
6+
"GUID:0acc523941302664db1f4e527237feb3",
7+
"GUID:27619889b8ba8c24980f49ee34dbb44a"
8+
],
9+
"includePlatforms": [
10+
"Editor"
11+
],
12+
"excludePlatforms": [],
13+
"allowUnsafeCode": true,
14+
"overrideReferences": true,
15+
"precompiledReferences": [
16+
"nunit.framework.dll"
17+
],
18+
"autoReferenced": false,
19+
"defineConstraints": [
20+
"UNITY_INCLUDE_TESTS"
21+
],
22+
"versionDefines": [],
23+
"noEngineReferences": false
24+
}

Common.Tests/UGUIDots.Common.Tests.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Common.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Common/NumericUtils.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Unity.Collections;
2+
using Unity.Mathematics;
3+
4+
namespace CarteDiem.Common {
5+
6+
public unsafe static class NumericUtils {
7+
8+
/// <summary>
9+
/// Fills a character array pointer with the unicode equivalent of each digit.
10+
/// </summary>
11+
/// <param name="ptr">The character buffer to store into</param>
12+
/// <param name="length">The size of the buffer</param>
13+
/// <param name="count">The total number of counted digits</param>
14+
public static void ToCharArray(this int value, char* ptr, in int length, out int count) {
15+
var offset = value < 0 ? 0 : -1;
16+
17+
value = math.abs(value);
18+
var stack = new NativeList<char>(Allocator.Temp);
19+
do {
20+
var mod = value % 10;
21+
value /= 10;
22+
23+
// Convert to the unicode numerical equivalent, the last digit should go into the last value
24+
stack.Add((char)(mod + 48));
25+
} while (value != 0);
26+
27+
for (int i = 0; i < stack.Length; i++) {
28+
var flipped = stack.Length + offset - i;
29+
30+
if (flipped < length) {
31+
ptr[flipped] = stack[i];
32+
}
33+
}
34+
35+
count = stack.Length;
36+
37+
if (offset >= 0) {
38+
ptr[0] = '-';
39+
count++;
40+
}
41+
42+
stack.Dispose();
43+
}
44+
45+
// TODO: Figure out how to count the number of digits in a floating point #.
46+
}
47+
}

Common/NumericUtils.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Common/UGUIDots.Common.asmdef

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "UGUIDots.Common",
3+
"references": [
4+
"GUID:e0cd26848372d4e5c891c569017e11f1",
5+
"GUID:d8b63aba1907145bea998dd612889d6b"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": true,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

0 commit comments

Comments
 (0)