Skip to content

Instantly share code, notes, and snippets.

@klkucan
Last active October 2, 2020 14:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klkucan/6c51468ca6f92933356477745f0a50e0 to your computer and use it in GitHub Desktop.
Save klkucan/6c51468ca6f92933356477745f0a50e0 to your computer and use it in GitHub Desktop.
this is a variant of string, it can be clear form memory.
using System.Collections;
using System.Collections.Generic;
public class UniqueString
{
// 'removable = false' means the string would be added to the global string pool
// which would stay in memory in the rest of the whole execution period.
public static string Intern(string str, bool removable = true)
{
if (str == null)
return null;
string ret = IsInterned(str);
if (ret != null)
return ret;
if (removable)
{
// the app-level interning (which could be cleared regularly)
m_strings.Add(str, str);
return str;
}
else
{
return string.Intern(str);
}
}
// Why return a ref rather than a bool?
// return-val is the ref to the unique interned one, which should be tested against `null`
public static string IsInterned(string str)
{
if (str == null)
return null;
string ret = string.IsInterned(str);
if (ret != null)
return ret;
if (m_strings.TryGetValue(str, out ret))
return ret;
return null;
}
// should be called on a regular basis
public static void Clear()
{
m_strings.Clear();
}
// Why use Dictionary?
// http://stackoverflow.com/questions/7760364/how-to-retrieve-actual-item-from-hashsett
private static Dictionary<string, string> m_strings = new Dictionary<string, string>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment