@@ -66,5 +66,71 @@ public static string Left(this string sValue, int iMaxLength)
6666 //Return the string
6767 return sValue ;
6868 }
69+
70+ public static string GetCleanName ( this string name , bool isAppName = false )
71+ {
72+ if ( string . IsNullOrWhiteSpace ( name ) ) return null ;
73+ /*
74+ if (EnvironmentMemoryCache[name] is string cachedEnvironment)
75+ {
76+ // Found in cache, quick return of already processed value
77+ // cached value may be empty string if all characters are foreign characters
78+ return string.IsNullOrWhiteSpace(cachedEnvironment) ? null : cachedEnvironment;
79+ }
80+
81+ if (isAppName)
82+ {
83+ if (ApplicationNameMemoryCache[name] is string cachedAppName)
84+ {
85+ // Found in cache, quick return of already processed value
86+ // cached value may be empty string if all characters are foreign characters
87+ return string.IsNullOrWhiteSpace(cachedAppName) ? null : cachedAppName;
88+ }
89+ }
90+ */
91+ var strippedName = RemoveDiacritics ( name ) ;
92+ var final = new List < char > ( ) ;
93+ var spaces = 0 ;
94+ for ( int i = 0 ; i < strippedName . Length ; i ++ )
95+ {
96+ var c = strippedName [ i ] ;
97+ if ( char . IsLetterOrDigit ( c ) || c == '_' || c == '-' || c == '.' )
98+ {
99+ final . Add ( c ) ;
100+ spaces = 0 ;
101+ }
102+ else if ( ( char . IsPunctuation ( c ) || c == ' ' ) && spaces == 0 && c != '\' ' )
103+ {
104+ final . Add ( ' ' ) ;
105+ ++ spaces ;
106+ }
107+ }
108+ var cleanName = string . Join ( "" , final ) . Trim ( ) ;
109+
110+ if ( isAppName )
111+ {
112+ //ApplicationNameMemoryCache[name] = cleanName;
113+ if ( string . IsNullOrWhiteSpace ( cleanName ) )
114+ {
115+ return string . Empty ;
116+ }
117+ }
118+ else
119+ {
120+ //EnvironmentMemoryCache[name] = cleanName;
121+ if ( string . IsNullOrWhiteSpace ( cleanName ) )
122+ {
123+ return string . Empty ;
124+ }
125+ }
126+
127+ return cleanName ;
128+ }
129+ // https://stackoverflow.com/a/2086575/8121383
130+ private static string RemoveDiacritics ( string name )
131+ {
132+ var tempBytes = Encoding . GetEncoding ( "ISO-8859-8" ) . GetBytes ( name ) ;
133+ return Encoding . UTF8 . GetString ( tempBytes ) ;
134+ }
69135 }
70136}
0 commit comments