/// <summary> /// Creates a new `ID` from a string representation (`space:key`, `key`, /// `@:key`). Will also create an `ID` from a localization key (`#space:key`, /// `#key`, `#@:key`). As opposed to using this method directly, it is /// recommended to use the more idiomatic explicit cast (`(ID)"foo:bar"`). /// </summary> /// <returns>The new ID.</returns> /// <param name="id">The string representation of the ID.</param> public static ID FromString(string id) { if (id.Count(':') > 1) { throw new InvalidIDException(id, "too many colon characters"); } var loc = false; if (id.StartsWithInvariant("#")) { loc = true; } var colon_idx = id.IndexOf(':'); if (colon_idx == -1) { if (!IsLaxIdentifier(new StringView(id, 0))) { throw new InvalidIDException(id, "keys must be alphanumeric identifiers"); } return(new ID { DefaultNamespace = true, Namespace = GUNGEON_NAMESPACE_VIEW, Name = new StringView(id, loc ? 1 : 0) }); } else { StringView nspace; var contextual = false; if (id.StartsWithInvariant("gungeon:")) { nspace = GUNGEON_NAMESPACE_VIEW; } else if (id.StartsWithInvariant("@:")) { nspace = CONTEXT_NAMESPACE_VIEW; contextual = true; } else { nspace = new StringView(id, loc ? 1 : 0, colon_idx); } var name = new StringView(id, colon_idx + (loc ? 2 : 1)); if (nspace != CONTEXT_NAMESPACE_VIEW && !IsLaxIdentifier(name)) { throw new InvalidIDException(id, "namespaces must be alphanumeric identifiers"); } if (!IsLaxIdentifier(name)) { throw new InvalidIDException(id, "names must be alphanumeric identifiers"); } return(new ID { Contextual = contextual, Namespace = nspace, Name = name }); } }
public static uint Compute(StringView s) { var hash = FNV_OFFSET_BASIS; for (var i = 0; i < s.Length; i++) { var octet = (byte)s[i]; hash = hash * FNV_PRIME; hash = hash ^ octet; } return(hash); }
internal static bool IsLaxIdentifier(StringView s) { if (s.Length == 0) { return(false); } if (!IsLaxIdentifierSymbol(s[0])) { return(false); } for (var i = 1; i < s.Length; i++) { var c = s[i]; if (!IsLaxIdentifierSymbol(c)) { return(false); } } return(true); }
public bool EqualsStringView(StringView v) { return(GetHashCodeUint() == v.GetHashCodeUint()); }
public LockedNamespaceException(StringView namesp) : base($"The ID namespace {namesp} is locked") { }