Skip to content

Commit 37f0a1c

Browse files
committed
addressed comments
1 parent 68d0cfd commit 37f0a1c

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

Assets/ECS_MLAgents_v0/Data/CharStruct.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ struct char<N> {
1515
private byte[] bytes;
1616
1717
public char<N>(string s){
18-
var buffer = System.Text.Encoding.ASCII.GetBytes(s);
19-
this.size = buffer.Length;
2018
this.bytes=new byte[<N>];
21-
if (size> <N>){
22-
size = <N>;
19+
this.size = s.Length;
20+
if (this.size > <N>){
21+
throw new NotSupportedException(
22+
"Cannot create a char<N> object with more than <N> characters"
23+
);
2324
}
24-
Array.Copy(buffer, 0, bytes, 0, size);
25+
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
2526
}
2627
2728
public string GetString(){
@@ -40,13 +41,14 @@ public struct char64 {
4041
private byte[] bytes;
4142

4243
public char64(string s){
43-
var buffer = System.Text.Encoding.ASCII.GetBytes(s);
44-
this.size = buffer.Length;
4544
this.bytes=new byte[64];
46-
if (size> 64){
47-
size = 64;
45+
this.size = s.Length;
46+
if (this.size > 64){
47+
throw new NotSupportedException(
48+
"Cannot create a char64 object with more than 64 characters"
49+
);
4850
}
49-
Array.Copy(buffer, 0, bytes, 0, size);
51+
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
5052
}
5153

5254
public string GetString(){
@@ -62,13 +64,14 @@ public struct char256 {
6264
private byte[] bytes;
6365

6466
public char256(string s){
65-
var buffer = System.Text.Encoding.ASCII.GetBytes(s);
66-
this.size = buffer.Length;
6767
this.bytes=new byte[256];
68-
if (size> 256){
69-
size = 256;
68+
this.size = s.Length;
69+
if (this.size > 256){
70+
throw new NotSupportedException(
71+
"Cannot create a char256 object with more than 256 characters"
72+
);
7073
}
71-
Array.Copy(buffer, 0, bytes, 0, size);
74+
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
7275
}
7376

7477
public string GetString(){

Assets/ECS_MLAgents_v0/Editor/Tests/CharStructTest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using ECS_MLAgents_v0.Data;
44
using System.Runtime.InteropServices;
55
using UnityEngine;
6+
using System;
67

78
namespace ECS_MLAgents_v0.Editor.Tests{
89
public class char64Test{
@@ -18,9 +19,9 @@ public void TestFromStringChar64(){
1819

1920
[Test]
2021
public void TestLongStringChar64(){
21-
var tooLong = new char64("0000000001000000000200000000030000000004000000000500000000060000XXXXX");
22-
Assert.AreEqual("0000000001000000000200000000030000000004000000000500000000060000",tooLong.GetString());
23-
}
22+
Assert.That(() => new char64("0000000001000000000200000000030000000004000000000500000000060000XXXXX"),
23+
Throws.TypeOf<NotSupportedException>());
24+
}
2425
}
2526

2627
public class char256Test{

0 commit comments

Comments
 (0)