/// <summary> /// Changes the extension of a string. /// </summary> /// <param name="id">The string to affect.</param> /// <param name="extension">The new extension with a leading period.</param> /// <returns>The id of a string with the new extension.</returns> public StringId ChangeExtension(StringId id, StringId extension) { Contract.Requires(IsValid()); Contract.Requires(id.IsValid); Contract.Requires(extension.IsValid); Contract.Ensures(Contract.Result <StringId>().IsValid); int originalLength = GetLength(id); int extLength = GetLength(extension); using (var wrapper = Pools.GetCharArray(originalLength + extLength)) { char[] name = wrapper.Instance; CopyString(id, name, 0); int newLength = originalLength; for (int i = originalLength; --i >= 0;) { char ch = name[i]; if (ch == '.') { newLength = i; break; } } CopyString(extension, name, newLength); var s = new CharArraySegment(name, 0, newLength + extLength); return(AddString(s)); } }
/// <summary> /// Concatenates two strings together. /// </summary> public StringId Concat(StringId id1, StringId id2) { Contract.Requires(id1.IsValid); Contract.Requires(id2.IsValid); Contract.Ensures(Contract.Result <StringId>().IsValid); int len1 = GetLength(id1); int len2 = GetLength(id2); using (var wrapper = Pools.GetCharArray(len1 + len2)) { char[] buffer = wrapper.Instance; CopyString(id1, buffer, 0); CopyString(id2, buffer, len1); var s = new CharArraySegment(buffer, 0, len1 + len2); return(AddString(s)); } }
/// <summary> /// Gets a string from this table. /// </summary> public string GetString(StringId id) { Contract.Requires(id.IsValid); Contract.Requires(IsValid()); if (!m_expansionCache.TryGetValue(id, out string result)) { int len = GetLength(id); using (var wrapper = Pools.GetCharArray(len)) { char[] buffer = wrapper.Instance; CopyString(id, buffer, 0); result = new string(buffer, 0, len); m_expansionCache.AddItem(id, result); } } return(result); }
/// <summary> /// Remove the extension of a path. /// </summary> /// <param name="id">The string affect.</param> /// <returns>The id of a path without its last extension.</returns> public StringId RemoveExtension(StringId id) { Contract.Requires(IsValid()); Contract.Requires(id.IsValid); Contract.Ensures(Contract.Result <StringId>().IsValid); int originalLength = GetLength(id); using (var wrapper = Pools.GetCharArray(originalLength)) { char[] name = wrapper.Instance; CopyString(id, name, 0); int newLength = originalLength; for (int i = originalLength; --i >= 0;) { char ch = name[i]; if (ch == '.') { newLength = i; break; } } if (newLength == 0) { // if we'd end up with an empty string just return the original return(id); } if (newLength == originalLength) { // no . found, return original string return(id); } var s = new CharArraySegment(name, 0, newLength); return(AddString(s)); } }
/// <summary> /// Gets the extension of a string. /// </summary> /// <param name="id">The string of which to extract the extension.</param> /// <returns>The extension.</returns> public StringId GetExtension(StringId id) { Contract.Requires(id.IsValid); int length = GetLength(id); using (var wrapper = Pools.GetCharArray(length)) { char[] name = wrapper.Instance; CopyString(id, name, 0); for (int i = length; --i >= 0;) { char ch = name[i]; if (ch == '.') { var s = new CharArraySegment(name, i, length - i); return(AddString(s)); } } return(StringId.Invalid); } }