55using System . IO ;
66using Newtonsoft . Json ;
77using Newtonsoft . Json . Linq ;
8+ using System . Xml . Linq ;
89
910namespace CodeGenerator
1011{
@@ -228,21 +229,35 @@ class EnumDefinition
228229 {
229230 private readonly Dictionary < string , string > _sanitizedNames ;
230231
231- public string Name { get ; }
232- public string FriendlyName { get ; }
232+ public string [ ] Names { get ; }
233+ public string [ ] FriendlyNames { get ; }
233234 public EnumMember [ ] Members { get ; }
234235
235236 public EnumDefinition ( string name , EnumMember [ ] elements )
236237 {
237- Name = name ;
238- if ( Name . EndsWith ( '_' ) )
238+ if ( TypeInfo . AlternateEnumPrefixes . TryGetValue ( name , out string altName ) )
239239 {
240- FriendlyName = Name . Substring ( 0 , Name . Length - 1 ) ;
240+ Names = new [ ] { name , altName } ;
241241 }
242242 else
243243 {
244- FriendlyName = Name ;
244+ Names = new [ ] { name } ;
245+ }
246+ FriendlyNames = new string [ Names . Length ] ;
247+ for ( int i = 0 ; i < Names . Length ; i ++ )
248+ {
249+ string n = Names [ i ] ;
250+ if ( n . EndsWith ( '_' ) )
251+ {
252+ FriendlyNames [ i ] = n . Substring ( 0 , n . Length - 1 ) ;
253+ }
254+ else
255+ {
256+ FriendlyNames [ i ] = n ;
257+ }
258+
245259 }
260+
246261 Members = elements ;
247262
248263 _sanitizedNames = new Dictionary < string , string > ( ) ;
@@ -265,12 +280,31 @@ public string SanitizeNames(string text)
265280 private string SanitizeMemberName ( string memberName )
266281 {
267282 string ret = memberName ;
268- if ( memberName . StartsWith ( Name ) )
283+ bool altSubstitution = false ;
284+
285+ // Try alternate substitution first
286+ foreach ( KeyValuePair < string , string > substitutionPair in TypeInfo . AlternateEnumPrefixSubstitutions )
269287 {
270- ret = memberName . Substring ( Name . Length ) ;
271- if ( ret . StartsWith ( "_" ) )
288+ if ( memberName . StartsWith ( substitutionPair . Key ) )
272289 {
273- ret = ret . Substring ( 1 ) ;
290+ ret = ret . Replace ( substitutionPair . Key , substitutionPair . Value ) ;
291+ altSubstitution = true ;
292+ break ;
293+ }
294+ }
295+
296+ if ( ! altSubstitution )
297+ {
298+ foreach ( string name in Names )
299+ {
300+ if ( memberName . StartsWith ( name ) )
301+ {
302+ ret = memberName . Substring ( name . Length ) ;
303+ if ( ret . StartsWith ( "_" ) )
304+ {
305+ ret = ret . Substring ( 1 ) ;
306+ }
307+ }
274308 }
275309 }
276310
@@ -279,7 +313,7 @@ private string SanitizeMemberName(string memberName)
279313 ret = ret . Substring ( 0 , ret . Length - 1 ) ;
280314 }
281315
282- if ( Char . IsDigit ( ret . First ( ) ) )
316+ if ( char . IsDigit ( ret . First ( ) ) )
283317 ret = "_" + ret ;
284318
285319 return ret ;
@@ -375,7 +409,7 @@ public TypeReference(string name, string type, int asize, string templateType, E
375409
376410 TypeVariants = typeVariants ;
377411
378- IsEnum = enums . Any ( t => t . Name == type || t . FriendlyName == type || TypeInfo . WellKnownEnums . Contains ( type ) ) ;
412+ IsEnum = enums . Any ( t => t . Names . Contains ( type ) || t . FriendlyNames . Contains ( type ) || TypeInfo . WellKnownEnums . Contains ( type ) ) ;
379413 }
380414
381415 private int ParseSizeString ( string sizePart , EnumDefinition [ ] enums )
@@ -394,7 +428,7 @@ private int ParseSizeString(string sizePart, EnumDefinition[] enums)
394428 {
395429 foreach ( EnumDefinition ed in enums )
396430 {
397- if ( sizePart . StartsWith ( ed . Name ) )
431+ if ( ed . Names . Any ( n => sizePart . StartsWith ( n ) ) )
398432 {
399433 foreach ( EnumMember member in ed . Members )
400434 {
0 commit comments