Advertisement
Rengaw

StringBuilderExtensions

Dec 17th, 2020
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using UnityEngine;
  4.  
  5. namespace Utility
  6. {
  7.     public static class StringBuilderExtensions
  8.     {
  9.         public const int DefaultSize = 12;
  10.        
  11.         public static readonly Color DefaultColor = new Color(1f, 0.5f, 0.0f);
  12.  
  13.         public static StringBuilder AppendBeginTag(this StringBuilder sb, string key)
  14.         {
  15.             return sb
  16.                 .Append('<')
  17.                 .Append(key)  
  18.                 .Append('>');
  19.         }
  20.  
  21.         public static StringBuilder AppendBeginTag(this StringBuilder sb, string key, string value)
  22.         {
  23.             return sb
  24.                 .Append('<')
  25.                 .Append(key)
  26.                 .Append('=')
  27.                 .Append(value)
  28.                 .Append('>');
  29.         }
  30.  
  31.         public static StringBuilder AppendEndTag(this StringBuilder sb, string key)
  32.         {
  33.             return sb
  34.                 .Append("</")
  35.                 .Append(key)
  36.                 .Append('>');
  37.         }
  38.        
  39.         public static StringBuilder AppendBeginSizeTag(this StringBuilder sb, int size = DefaultSize)
  40.         {
  41.             return sb.AppendBeginTag("size", size.ToString());
  42.         }
  43.  
  44.         public static StringBuilder AppendEndSizeTag(this StringBuilder sb)
  45.         {
  46.             return sb.AppendEndTag("size");
  47.         }
  48.  
  49.         public static StringBuilder AppendBeginColorTag(this StringBuilder sb, Color color)
  50.         {
  51.             return sb.AppendBeginTag("color", $"#{ColorUtility.ToHtmlStringRGB(color)}");
  52.         }
  53.  
  54.         public static StringBuilder AppendEndColorTag(this StringBuilder sb)
  55.         {
  56.             return sb.AppendEndTag("color");
  57.         }
  58.  
  59.         public static StringBuilder AppendWithColor(this StringBuilder sb, string value, Color color = default)
  60.         {
  61.             if (color == default) color = DefaultColor;
  62.             return sb
  63.                 .AppendBeginColorTag(color)
  64.                 .Append(value)
  65.                 .AppendEndColorTag();
  66.         }
  67.  
  68.         public static StringBuilder Log(this StringBuilder sb)
  69.         {
  70.             Debug.Log(sb.ToString());
  71.             return sb;
  72.         }
  73.  
  74.         public static StringBuilder Log(this StringBuilder sb, params string[] tags)
  75.         {
  76.             sb.AppendLine(Environment.NewLine);
  77.  
  78.             foreach (var tag in tags)
  79.                 sb.Append('[').Append(tag).Append("] ");
  80.  
  81.             return sb.AppendLine().Log();
  82.         }
  83.  
  84.         public static string AcquireAndClear(this StringBuilder sb)
  85.         {
  86.             var value = sb.ToString();
  87.             sb.Clear();
  88.             return value;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement