/
XmlDictionaryString.cs
141 lines (121 loc) · 3.78 KB
/
XmlDictionaryString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Xml;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;
namespace System.Xml
{
public class XmlDictionaryString
{
internal const int MinKey = 0;
internal const int MaxKey = int.MaxValue / 4;
private readonly IXmlDictionary _dictionary;
private readonly string _value;
private readonly int _key;
private byte[]? _buffer;
private static readonly EmptyStringDictionary s_emptyStringDictionary = new EmptyStringDictionary();
public XmlDictionaryString(IXmlDictionary dictionary, string value, int key)
{
ArgumentNullException.ThrowIfNull(dictionary);
ArgumentNullException.ThrowIfNull(value);
if (key < MinKey || key > MaxKey)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(key), SR.Format(SR.ValueMustBeInRange, MinKey, MaxKey)));
_dictionary = dictionary;
_value = value;
_key = key;
}
[return: NotNullIfNotNull(nameof(s))]
internal static string? GetString(XmlDictionaryString? s)
{
if (s == null)
return null;
return s.Value;
}
public static XmlDictionaryString Empty
{
get
{
return s_emptyStringDictionary.EmptyString;
}
}
public IXmlDictionary Dictionary
{
get
{
return _dictionary;
}
}
public int Key
{
get
{
return _key;
}
}
public string Value
{
get
{
return _value;
}
}
internal byte[] ToUTF8()
{
return _buffer ??= System.Text.Encoding.UTF8.GetBytes(_value);
}
public override string ToString()
{
return _value;
}
private sealed class EmptyStringDictionary : IXmlDictionary
{
private readonly XmlDictionaryString _empty;
public EmptyStringDictionary()
{
_empty = new XmlDictionaryString(this, string.Empty, 0);
}
public XmlDictionaryString EmptyString
{
get
{
return _empty;
}
}
public bool TryLookup(string value, [NotNullWhen(true)] out XmlDictionaryString? result)
{
ArgumentNullException.ThrowIfNull(value);
if (value.Length == 0)
{
result = _empty;
return true;
}
result = null;
return false;
}
public bool TryLookup(int key, [NotNullWhen(true)] out XmlDictionaryString? result)
{
if (key == 0)
{
result = _empty;
return true;
}
result = null;
return false;
}
public bool TryLookup(XmlDictionaryString value, [NotNullWhen(true)] out XmlDictionaryString? result)
{
ArgumentNullException.ThrowIfNull(value);
if (value.Dictionary != this)
{
result = null;
return false;
}
result = value;
return true;
}
}
}
}