From a2bd99f2afadab352b1cbce47827ee228f2444ba Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 15 Jan 2024 19:31:57 +0000 Subject: [PATCH 1/4] ToGuid and FromGuid --- CBOR/PeterO/Cbor/CBORObject.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 2546ec5c..3c73fbfd 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -2332,6 +2332,12 @@ public static CBORObject FromInt32(int value) { FromInt64((long)value); } + /// Generates a CBOR object from a Guid. + /// The parameter is a + /// Guid. + /// A CBOR object. + public static CBORObject FromGuid(Guid value) => new CBORUuidConverter().ToCBORObject(value); + /// Generates a CBOR object from a 32-bit signed /// integer. /// The parameter is a @@ -4661,6 +4667,14 @@ public float AsSingle() { return cn.GetNumberInterface().AsSingle(cn.GetValue()); } + /// Converts this object to a Guid. + /// A Guid. + /// This object does + /// not represent a Guid. + public Guid AsGuid() { + return new CBORUuidConverter().FromCBORObject(this); + } + /// Gets the value of this object as a text string. /// Gets this object's string. /// This object's type is From 6ed77974cfa31136803d62a3adf813f13b77999c Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 15 Jan 2024 19:34:06 +0000 Subject: [PATCH 2/4] CBORException can be thrown too --- CBOR/PeterO/Cbor/CBORObject.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 3c73fbfd..94f3cc94 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -4670,7 +4670,8 @@ public float AsSingle() { /// Converts this object to a Guid. /// A Guid. /// This object does - /// not represent a Guid. + /// not represent a Guid. + /// This object does not have the expected tag. public Guid AsGuid() { return new CBORUuidConverter().FromCBORObject(this); } From b45ae36bcb4900da8e07650341c8e1edd3598901 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 15 Jan 2024 21:00:06 +0000 Subject: [PATCH 3/4] don't create a string when converting from bytes --- CBOR/PeterO/Cbor/CBORUuidConverter.cs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORUuidConverter.cs b/CBOR/PeterO/Cbor/CBORUuidConverter.cs index 55eb9668..8f02b10c 100644 --- a/CBOR/PeterO/Cbor/CBORUuidConverter.cs +++ b/CBOR/PeterO/Cbor/CBORUuidConverter.cs @@ -34,19 +34,9 @@ public Guid FromCBORObject(CBORObject obj) { throw new CBORException("Must have outermost tag 37"); } _ = ValidateObject(obj); - byte[] bytes = obj.GetByteString(); - var guidChars = new char[36]; - string hex = "0123456789abcdef"; - var index = 0; - for (int i = 0; i < 16; ++i) { - if (i == 4 || i == 6 || i == 8 || i == 10) { - guidChars[index++] = '-'; - } - guidChars[index++] = hex[(bytes[i] >> 4) & 15]; - guidChars[index++] = hex[bytes[i] & 15]; - } - var guidString = new String(guidChars); - return new Guid(guidString); + byte[] b2 = obj.GetByteString(); + byte[] bytes = { b2[3], b2[2], b2[1], b2[0], b2[5], b2[4], b2[7], b2[6], b2[8], b2[9], b2[10], b2[11], b2[12], b2[13], b2[14], b2[15] }; + return new Guid(bytes); } } } From 7e31d06db6b49a6c28895f93bed9ebd037aad6fd Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 22 Jan 2024 12:49:03 +0000 Subject: [PATCH 4/4] add FromMap and FromOrderedMap --- CBOR/PeterO/Cbor/CBORObject.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 94f3cc94..4d685168 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -3236,6 +3236,20 @@ public static CBORObject NewMap() { new SortedDictionary()); } + /// Creates a new CBOR map that stores its keys in an + /// undefined order. + /// A sequence of key-value pairs. + /// A new CBOR map. + public static CBORObject FromMap(IEnumerable> keysAndValues) { + var sd = new SortedDictionary(); + foreach (Tuple kv in keysAndValues) { + sd.Add(kv.Item1, kv.Item2); + } + return new CBORObject( + CBORObjectTypeMap, + sd); + } + /// Creates a new empty CBOR map that ensures that keys are /// stored in the order in which they are first inserted. /// A new CBOR map. @@ -3245,6 +3259,20 @@ public static CBORObject NewOrderedMap() { PropertyMap.NewOrderedDict()); } + /// Creates a new CBOR map that ensures that keys are + /// stored in order. + /// A sequence of key-value pairs. + /// A new CBOR map. + public static CBORObject FromOrderedMap(IEnumerable> keysAndValues) { + var oDict = PropertyMap.NewOrderedDict(); + foreach (Tuple kv in keysAndValues) { + oDict.Add(kv.Item1, kv.Item2); + } + return new CBORObject( + CBORObjectTypeMap, + oDict); + } + /// /// Reads a sequence of objects in CBOR format from a data /// stream. This method will read CBOR objects from the stream until