ExtentionMethod.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace Edge.Core
  8. {
  9. public static class ExtentionMethod
  10. {
  11. /// <summary>
  12. /// Extract the localized content string from source, the source should follow the format-> lang-zh-cn:稳Dart加油机lang-en-us:WayneDartPump
  13. /// </summary>
  14. /// <param name="source">should follow the format-> lang-zh-cn:稳Dart加油机lang-en-us:WayneDartPumplang-xx:xContent</param>
  15. /// <param name="language">like-> en-us, zh-cn, or en, zh</param>
  16. /// <returns>if target language is not searched from source, then content with 'en' locale will be returned, if 'en' locale is not searched, content with 'zh-cn' locale will be returned original, otherwise source will be returned</returns>
  17. public static string LocalizedContent(this string source, string language)
  18. {
  19. if (string.IsNullOrEmpty(source)) return source;
  20. //if (string.IsNullOrEmpty(language)) return source;
  21. //var langDeclareRegexStr = @"(lang-\w\w-\w\w\:).+?((?=lang-\w\w-\w\w\:)|(?=$))";
  22. var allLangDeclareRegexStr = @"(lang-\w\w(-\w\w)?\:).+?((?=lang-\w\w(-\w\w)?\:)|(?=$))";
  23. var allLangMatches = Regex.Matches(source, allLangDeclareRegexStr).Cast<Match>();
  24. foreach (var langMatch in allLangMatches)
  25. {
  26. var targetLangPrefix = "^lang-" + language + @"(-\w\w)?\:";
  27. var m = Regex.Match(langMatch.Value, targetLangPrefix, RegexOptions.IgnoreCase);
  28. if (m.Success)
  29. return langMatch.Value.Substring(m.Length);
  30. }
  31. var default_match =
  32. allLangMatches.FirstOrDefault(m =>
  33. m.Value.Split(':')[0].Contains("en", StringComparison.OrdinalIgnoreCase));
  34. if (default_match == null)
  35. default_match = allLangMatches.FirstOrDefault(m =>
  36. m.Value.Split(':')[0].Contains("zh-cn", StringComparison.OrdinalIgnoreCase));
  37. return default_match?.Value?.Substring(default_match.Value.Split(':')[0].Length + 1) ?? source;
  38. }
  39. /// <summary>
  40. /// Extract the localized content string from the json schema source.
  41. /// the multi-language text in json schema is like-> "title": "lang-zh-cn:停止位lang-en-us:StopBits"
  42. /// </summary>
  43. /// <param name="jsonSchemaSource"></param>
  44. /// <param name="language">like-> en-us, zh-cn, or en, zh</param>
  45. /// <returns></returns>
  46. public static string LocalizedJsonSchema(this string jsonSchemaSource, string language)
  47. {
  48. if (string.IsNullOrEmpty(jsonSchemaSource)) return jsonSchemaSource;
  49. //if (string.IsNullOrEmpty(language)) return jsonSchemaSource;
  50. // sample: "title": "lang-zh-cn:停止位lang-en-us:StopBits"
  51. var targetContentRegexStr = @"(?<=\""(title|infoText)\""\s*?\:\s*?\"").+?((?=\"")|(?=$))";
  52. var matches = Regex.Matches(jsonSchemaSource, targetContentRegexStr, RegexOptions.Multiline | RegexOptions.IgnoreCase).Cast<Match>();
  53. foreach (var ma in matches)
  54. {
  55. jsonSchemaSource = jsonSchemaSource.Replace(ma.Value, ma.Value.LocalizedContent(language));
  56. }
  57. //var allInfoTextContentRegexStr = @"(?<=\""infoText\""\s*?\:\s*?\"").+?((?=\"")|(?=$))";
  58. //var allInfoTextContentMatches = Regex.Matches(jsonSchemaSource, allInfoTextContentRegexStr, RegexOptions.Multiline | RegexOptions.IgnoreCase).Cast<Match>();
  59. //foreach (var infoTextNodeContentMatch in allInfoTextContentMatches)
  60. //{
  61. // jsonSchemaSource =
  62. // jsonSchemaSource.Replace(infoTextNodeContentMatch.Value,
  63. // infoTextNodeContentMatch.Value.LocalizedContent(language));
  64. //}
  65. return jsonSchemaSource;
  66. }
  67. /// <summary>
  68. /// Search the target byte list in src.
  69. /// </summary>
  70. /// <param name="src"></param>
  71. /// <param name="target"></param>
  72. /// <returns>-1 indicates can not find target in src, otherwise, return the index in src, 0 based.</returns>
  73. public static int IndexOfSubList(this List<byte> src, List<byte> target)
  74. {
  75. if (src.Count < target.Count) return -1;
  76. int everFound = 0;
  77. int srcIndex = 0;
  78. int targetIndex = 0;
  79. for (int i = 0; i < src.Count; i++)
  80. {
  81. //if (srcIndex == src.Count - 1)
  82. //{
  83. // if (everFound == target.Count) return srcIndex - target.Count + 1;
  84. // else return -1;
  85. //}
  86. if (everFound == target.Count) return srcIndex - target.Count;
  87. if (src[srcIndex] == target[targetIndex])
  88. {
  89. everFound++;
  90. srcIndex++;
  91. targetIndex++;
  92. }
  93. else
  94. {
  95. if (everFound > 0)
  96. { targetIndex = 0; everFound = 0; }
  97. else
  98. {
  99. srcIndex++;
  100. }
  101. }
  102. }
  103. if (everFound == target.Count) return srcIndex - target.Count;
  104. return -1;
  105. }
  106. /// <summary>
  107. /// create a custom logger if it does not exists, otherwise get the logger.
  108. /// This is only for NLog implementation.
  109. /// </summary>
  110. /// <param name="loggerFactory"></param>
  111. /// <param name="loggerFileName">custom logger file name will be created or get.</param>
  112. /// <returns></returns>
  113. public static ILogger CreateOrGetCustomLogger(this ILoggerFactory loggerFactory, string loggerFileName, LogLevel logLevel)
  114. {
  115. if (string.IsNullOrEmpty(loggerFileName)) throw new ArgumentException("loggerFileName must not be null or empty");
  116. ILogger logger = null;
  117. switch (logLevel)
  118. {
  119. case LogLevel.Trace:
  120. logger = loggerFactory.CreateLogger("DynamicPrivate_Trace_" + loggerFileName);
  121. break;
  122. case LogLevel.Debug:
  123. logger = loggerFactory.CreateLogger("DynamicPrivate_Debug_" + loggerFileName);
  124. break;
  125. case LogLevel.Information:
  126. logger = loggerFactory.CreateLogger("DynamicPrivate_Info_" + loggerFileName);
  127. break;
  128. case LogLevel.Warning:
  129. logger = loggerFactory.CreateLogger("DynamicPrivate_Warn_" + loggerFileName);
  130. break;
  131. case LogLevel.Error:
  132. logger = loggerFactory.CreateLogger("DynamicPrivate_Error_" + loggerFileName);
  133. break;
  134. case LogLevel.Critical:
  135. logger = loggerFactory.CreateLogger("DynamicPrivate_Fatal_" + loggerFileName);
  136. break;
  137. }
  138. return logger;
  139. }
  140. }
  141. }