Skip to content

Commit

Permalink
generalize such that verbatim strings always return just the value po…
Browse files Browse the repository at this point in the history
…rtion unless explicitly requested
  • Loading branch information
mgravell committed Aug 16, 2023
1 parent 7efe5f8 commit cf956c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
43 changes: 23 additions & 20 deletions src/StackExchange.Redis/RawResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,18 @@ private static GeoPosition AsGeoPosition(in Sequence<RawResult> coords)
internal GeoPosition?[]? GetItemsAsGeoPositionArray()
=> this.ToArray<GeoPosition?>((in RawResult item) => item.IsNull ? default : AsGeoPosition(item.GetItems()));

internal unsafe string? GetString()
internal unsafe string? GetString() => GetString(out _);
internal unsafe string? GetString(out ReadOnlySpan<char> verbatimPrefix)
{
verbatimPrefix = default;
if (IsNull) return null;
if (Payload.IsEmpty) return "";

string s;
if (Payload.IsSingleSegment)
{
return Format.GetString(Payload.First.Span);
s = Format.GetString(Payload.First.Span);
return Resp3Type == ResultType.VerbatimString ? GetVerbatimString(s, out verbatimPrefix) : s;
}
var decoder = Encoding.UTF8.GetDecoder();
int charCount = 0;
Expand All @@ -409,7 +413,7 @@ private static GeoPosition AsGeoPosition(in Sequence<RawResult> coords)

decoder.Reset();

string s = new string((char)0, charCount);
s = new string((char)0, charCount);
fixed (char* sPtr = s)
{
char* cPtr = sPtr;
Expand All @@ -426,26 +430,25 @@ private static GeoPosition AsGeoPosition(in Sequence<RawResult> coords)
}
}
}
return s;
}
return Resp3Type == ResultType.VerbatimString ? GetVerbatimString(s, out verbatimPrefix) : s;

internal string? GetVerbatimString(out ReadOnlySpan<char> type)
{
// the first three bytes provide information about the format of the following string, which
// can be txt for plain text, or mkd for markdown. The fourth byte is always `:`
// Then the real string follows.
var value = GetString();
if (value is not null && Resp3Type == ResultType.VerbatimString
&& value.Length >= 4 && value[3] == ':')
{
type = value.AsSpan().Slice(0, 3);
value = value.Substring(4);
}
else
static string? GetVerbatimString(string? value, out ReadOnlySpan<char> type)
{
type = default;
// the first three bytes provide information about the format of the following string, which
// can be txt for plain text, or mkd for markdown. The fourth byte is always `:`
// Then the real string follows.
if (value is not null
&& value.Length >= 4 && value[3] == ':')
{
type = value.AsSpan().Slice(0, 3);
value = value.Substring(4);
}
else
{
type = default;
}
return value;
}
return value;
}

internal bool TryGetDouble(out double val)
Expand Down
2 changes: 1 addition & 1 deletion src/StackExchange.Redis/ResultProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
{
string category = Normalize(null);
var list = new List<Tuple<string, KeyValuePair<string, string>>>();
var raw = result.GetVerbatimString(out _);
var raw = result.GetString();
if (raw is not null)
{
using var reader = new StringReader(raw);
Expand Down

0 comments on commit cf956c1

Please sign in to comment.