Barotrauma Client Doc
Identifier.cs
1 #nullable enable
2 
3 using System;
4 using System.Collections.Generic;
5 using System.Diagnostics.CodeAnalysis;
6 using System.Linq;
7 
8 namespace Barotrauma
9 {
10  // Identifier struct to eliminate case-sensitive comparisons
11  public readonly struct Identifier : IComparable, IEquatable<Identifier>
12  {
13  public readonly static Identifier Empty = default;
14 
15  private readonly static int emptyHash = "".GetHashCode(StringComparison.OrdinalIgnoreCase);
16 
17  private readonly string? value;
18  private readonly Lazy<int>? hashCode;
19 
20  public string Value => value ?? "";
21  public int HashCode => hashCode?.Value ?? emptyHash;
22 
23  public Identifier(string? str)
24  {
25  value = str;
26  hashCode = new Lazy<int>(() => (str ?? "").GetHashCode(StringComparison.OrdinalIgnoreCase));
27  }
28 
29  public bool IsEmpty => Value.IsNullOrEmpty();
30 
32  => IsEmpty ? id : this;
33 
34  public Identifier Replace(in Identifier subStr, in Identifier newStr)
35  => Replace(subStr.Value ?? "", newStr.Value ?? "");
36 
37  public Identifier Replace(string subStr, string newStr)
38  => (Value?.Replace(subStr, newStr, StringComparison.OrdinalIgnoreCase)).ToIdentifier();
39 
40  public Identifier Remove(Identifier subStr)
41  => Remove(subStr.Value ?? "");
42 
43  public Identifier Remove(string subStr)
44  => (Value?.Remove(subStr, StringComparison.OrdinalIgnoreCase)).ToIdentifier();
45 
46  public override bool Equals(object? obj) =>
47  obj switch
48  {
49  Identifier i => this == i,
50  string s => this == s,
51  _ => base.Equals(obj)
52  };
53 
54  public bool StartsWith(string str) => Value?.StartsWith(str, StringComparison.OrdinalIgnoreCase) ?? str.IsNullOrEmpty();
55 
56  public bool StartsWith(Identifier id) => StartsWith(id.Value ?? "");
57 
58  public bool EndsWith(string str) => Value?.EndsWith(str, StringComparison.OrdinalIgnoreCase) ?? str.IsNullOrEmpty();
59 
60  public bool EndsWith(Identifier id) => EndsWith(id.Value ?? "");
61 
62  public Identifier AppendIfMissing(string suffix)
63  => EndsWith(suffix) ? this : $"{this}{suffix}".ToIdentifier();
64 
65  public Identifier RemoveFromEnd(string suffix)
66  => (Value?.RemoveFromEnd(suffix, StringComparison.OrdinalIgnoreCase)).ToIdentifier();
67 
68  public bool Contains(string str) => Value?.Contains(str, StringComparison.OrdinalIgnoreCase) ?? str.IsNullOrEmpty();
69 
70  public bool Contains(in Identifier id) => Contains(id.Value ?? "");
71 
72  public override string ToString() => Value ?? "";
73 
74  public override int GetHashCode() => HashCode;
75 
76  public int CompareTo(object? obj)
77  {
78  return string.Compare(Value, obj?.ToString() ?? "", StringComparison.InvariantCultureIgnoreCase);
79  }
80 
81  public bool Equals([AllowNull] Identifier other)
82  {
83  return this == other;
84  }
85 
86  private static bool StringEquality(string? a, string? b)
87  => (a.IsNullOrEmpty() && b.IsNullOrEmpty()) || string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
88 
89  public static bool operator ==(in Identifier a, in Identifier b) =>
90  StringEquality(a.Value, b.Value);
91 
92  public static bool operator !=(in Identifier a, in Identifier b) =>
93  !(a == b);
94 
95  public static bool operator ==(in Identifier identifier, string? str) =>
96  StringEquality(identifier.Value, str);
97 
98  public static bool operator !=(in Identifier identifier, string? str) =>
99  !(identifier == str);
100 
101  public static bool operator ==(string? str, in Identifier identifier) =>
102  identifier == str;
103 
104  public static bool operator !=(string? str, in Identifier identifier) =>
105  !(identifier == str);
106 
107  public static bool operator ==(in Identifier? a, in Identifier? b) =>
108  StringEquality(a?.Value, b?.Value);
109 
110  public static bool operator !=(in Identifier? a, in Identifier? b) =>
111  !(a == b);
112 
113  public static bool operator ==(in Identifier? a, string? b) =>
114  StringEquality(a?.Value, b);
115 
116  public static bool operator !=(in Identifier? a, string? b) =>
117  !(a == b);
118 
119  public static bool operator ==(string str, in Identifier? identifier) =>
120  identifier == str;
121 
122  public static bool operator !=(string str, in Identifier? identifier) =>
123  !(identifier == str);
124 
125  public static implicit operator Identifier(string str)
126  {
127  return new Identifier(str);
128  }
129  internal int IndexOf(char c) => Value.IndexOf(c);
130 
131  internal Identifier this[Range range] => Value[range].ToIdentifier();
132  internal Char this[int i] => Value[i];
133  }
134 
135  public static class IdentifierExtensions
136  {
137  public static IEnumerable<Identifier> ToIdentifiers(this IEnumerable<string> strings)
138  {
139  foreach (string s in strings)
140  {
141  if (string.IsNullOrEmpty(s)) { continue; }
142  yield return new Identifier(s);
143  }
144  }
145 
146  public static Identifier[] ToIdentifiers(this string[] strings)
147  => ((IEnumerable<string>)strings).ToIdentifiers().ToArray();
148 
149  public static Identifier ToIdentifier(this string? s)
150  {
151  return new Identifier(s);
152  }
153 
154  public static Identifier ToIdentifier<T>(this T t) where T: notnull
155  {
156  return t.ToString().ToIdentifier();
157  }
158 
159  public static bool Contains(this ISet<Identifier> set, string identifier)
160  {
161  return set.Contains(identifier.ToIdentifier());
162  }
163 
164  public static bool ContainsKey<T>(this IReadOnlyDictionary<Identifier, T> dictionary, string key)
165  {
166  return dictionary.ContainsKey(key.ToIdentifier());
167  }
168  }
169 }
bool StartsWith(string str)
bool Contains(in Identifier id)
bool EndsWith(Identifier id)
bool Equals([AllowNull] Identifier other)
Definition: Identifier.cs:81
override bool Equals(object? obj)
Identifier IfEmpty(in Identifier id)
Identifier RemoveFromEnd(string suffix)
Identifier AppendIfMissing(string suffix)
bool Contains(string str)
static bool operator!=(in Identifier a, in Identifier b)
Identifier Replace(in Identifier subStr, in Identifier newStr)
override string ToString()
bool StartsWith(Identifier id)
override int GetHashCode()
Identifier Replace(string subStr, string newStr)
static bool operator==(in Identifier a, in Identifier b)
int CompareTo(object? obj)
Definition: Identifier.cs:76
Identifier(string? str)
Definition: Identifier.cs:23
static readonly Identifier Empty
Definition: Identifier.cs:13
Identifier Remove(string subStr)
bool EndsWith(string str)
Identifier Remove(Identifier subStr)
An inclusive range, i.e. [Start, End] where Start <= End
Definition: Range.cs:10