using System; using System.Collections.Generic; using System.Text; namespace Superlazy { public static class Utility { public static bool IsNullOrFalse(this SLEntity entity) { return entity == null || entity.IsExist() == false; } public static string CombinePath(this string first, string add) { if (first == null || first == string.Empty) { return add; } else if (add == null || add == string.Empty) { return first; } else { return new StringBuilder().Append(first).Append('.').Append(add).ToString(); } } public static void RemoveIf(this SLEntity entity, Func onRemove) { var removes = new List(); foreach (var e in entity) { if (onRemove(e)) removes.Add(e.ID); } foreach (var r in removes) { entity[r] = false; } } public static bool Contains(this SLEntity entity, string value) { return entity.ToString().Contains(value); } public static SLEntity Get(this SLEntity entity, string path) { if (entity == null) return false; var list = entity; var oldIdx = 0; var idx = path.IndexOf('.'); var len = path.Length; while (idx != -1 && oldIdx < len) { { var sub = path.Substring(oldIdx, idx - oldIdx); if (string.IsNullOrEmpty(sub) == false) list = list[sub]; } oldIdx = idx + 1; if (idx + 1 >= path.Length) { idx = -1; } else { idx = path.IndexOf('.', idx + 1); } } if (oldIdx < len) { var sub = path.Substring(oldIdx); if (string.IsNullOrEmpty(sub) == false) list = list[sub]; } return list; } public static void Set(this SLEntity entity, string path, SLEntity value) { var list = entity; var oldIdx = 0; var idx = path.IndexOf('.'); var len = path.Length; if (idx == -1) { list[path] = value; return; } while (idx != -1 && oldIdx < len) { { var sub = path.Substring(oldIdx, idx - oldIdx); if (string.IsNullOrEmpty(sub) == false) list = list[sub]; } oldIdx = idx + 1; if (idx + 1 >= path.Length) { idx = -1; } else { idx = path.IndexOf('.', idx + 1); } } if (oldIdx < len) { var sub = path.Substring(oldIdx); if (string.IsNullOrEmpty(sub) == false) { list[sub] = value; } else { SLLog.Error($"Set Can't end with . : {path}"); } } } public static bool IsLeft(this SLEntity entity, string value) { return entity.ToString().IsLeft(value); } public static bool IsLeft(this string str, string value) { if (str.Length >= value.Length) { for (var i = 0; i < value.Length; i++) { if (str[i] != value[i]) return false; } return true; } return false; } public static bool IsRight(this SLEntity entity, string value) { return entity.ToString().IsRight(value); } public static bool IsRight(this string str, string value) { if (str.Length >= value.Length) { for (var i = 1; i <= value.Length; ++i) { if (str[str.Length - i] != value[value.Length - i]) return false; } return true; } return false; } } }