TemplateUtil.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace HybridCLR.Generators
  7. {
  8. public static class TemplateUtil
  9. {
  10. public static string EscapeIntegerName(int i)
  11. {
  12. return i >= 0 ? i.ToString() : "minus" + (-i);
  13. }
  14. public static string ReplaceRegion(string resultText, string region, string replaceContent)
  15. {
  16. int startIndex = resultText.IndexOf("//!!!{{" + region);
  17. if (startIndex == -1)
  18. {
  19. throw new Exception($"region:{region} start not find");
  20. }
  21. int endIndex = resultText.IndexOf("//!!!}}" + region);
  22. if (endIndex == -1)
  23. {
  24. throw new Exception($"region:{region} end not find");
  25. }
  26. int replaceStart = resultText.IndexOf('\n', startIndex);
  27. int replaceEnd = resultText.LastIndexOf('\n', endIndex);
  28. if (replaceStart == -1 || replaceEnd == -1)
  29. {
  30. throw new Exception($"region:{region} not find");
  31. }
  32. resultText = resultText.Substring(0, replaceStart) + "\n" + replaceContent + "\n" + resultText.Substring(replaceEnd);
  33. return resultText;
  34. }
  35. }
  36. }