Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose FromGuid, AsGuid, and FromMap, and make AsGuid more efficient #73

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions CBOR/PeterO/Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,12 @@ public static CBORObject FromInt32(int value) {
FromInt64((long)value);
}

/// <summary>Generates a CBOR object from a Guid.</summary>
/// <param name='value'>The parameter <paramref name='value'/> is a
/// Guid.</param>
/// <returns>A CBOR object.</returns>
public static CBORObject FromGuid(Guid value) => new CBORUuidConverter().ToCBORObject(value);

/// <summary>Generates a CBOR object from a 32-bit signed
/// integer.</summary>
/// <param name='value'>The parameter <paramref name='value'/> is a
Expand Down Expand Up @@ -3230,6 +3236,20 @@ public static CBORObject NewMap() {
new SortedDictionary<CBORObject, CBORObject>());
}

/// <summary>Creates a new CBOR map that stores its keys in an
/// undefined order.</summary>
/// <param name='keysAndValues'>A sequence of key-value pairs.</param>
/// <returns>A new CBOR map.</returns>
public static CBORObject FromMap(IEnumerable<Tuple<CBORObject, CBORObject>> keysAndValues) {
var sd = new SortedDictionary<CBORObject, CBORObject>();
foreach (Tuple<CBORObject, CBORObject> kv in keysAndValues) {
sd.Add(kv.Item1, kv.Item2);
}
return new CBORObject(
CBORObjectTypeMap,
sd);
}

/// <summary>Creates a new empty CBOR map that ensures that keys are
/// stored in the order in which they are first inserted.</summary>
/// <returns>A new CBOR map.</returns>
Expand All @@ -3239,6 +3259,20 @@ public static CBORObject NewOrderedMap() {
PropertyMap.NewOrderedDict());
}

/// <summary>Creates a new CBOR map that ensures that keys are
/// stored in order.</summary>
/// <param name='keysAndValues'>A sequence of key-value pairs.</param>
/// <returns>A new CBOR map.</returns>
public static CBORObject FromOrderedMap(IEnumerable<Tuple<CBORObject, CBORObject>> keysAndValues) {
var oDict = PropertyMap.NewOrderedDict();
foreach (Tuple<CBORObject, CBORObject> kv in keysAndValues) {
oDict.Add(kv.Item1, kv.Item2);
}
return new CBORObject(
CBORObjectTypeMap,
oDict);
}

/// <summary>
/// <para>Reads a sequence of objects in CBOR format from a data
/// stream. This method will read CBOR objects from the stream until
Expand Down Expand Up @@ -4661,6 +4695,15 @@ public float AsSingle() {
return cn.GetNumberInterface().AsSingle(cn.GetValue());
}

/// <summary>Converts this object to a Guid.</summary>
/// <returns>A Guid.</returns>
/// <exception cref="InvalidOperationException">This object does
/// not represent a Guid.</exception><exception cref="CBORException">
/// This object does not have the expected tag.</exception>
public Guid AsGuid() {
return new CBORUuidConverter().FromCBORObject(this);
}

/// <summary>Gets the value of this object as a text string.</summary>
/// <returns>Gets this object's string.</returns>
/// <exception cref='InvalidOperationException'>This object's type is
Expand Down
16 changes: 3 additions & 13 deletions CBOR/PeterO/Cbor/CBORUuidConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Loading