From 5ec28b65be017c4aa735fdb35636f48b6510414d Mon Sep 17 00:00:00 2001 From: thefiddler Date: Thu, 14 Aug 2014 10:30:14 +0200 Subject: [PATCH] [Bind] Do not re-translate translated enum names --- Source/Bind/EnumProcessor.cs | 120 ++++++++++++++++++++++++------------------- Source/Bind/Utilities.cs | 67 +++++++++++++++--------- Source/Bind/XmlSpecReader.cs | 2 +- 3 files changed, 111 insertions(+), 78 deletions(-) diff --git a/Source/Bind/EnumProcessor.cs b/Source/Bind/EnumProcessor.cs index 0b0c189..2983d95 100644 --- a/Source/Bind/EnumProcessor.cs +++ b/Source/Bind/EnumProcessor.cs @@ -139,6 +139,17 @@ namespace Bind return name; } + static bool IsAlreadyProcessed(string name) + { + string extension = Utilities.GetExtension(name, true); + bool unprocessed = false; + unprocessed |= name.Contains("_") || name.Contains("-"); + unprocessed |= Char.IsDigit(name[0]); + unprocessed |= name.All(c => Char.IsUpper(c)); + unprocessed |= !String.IsNullOrEmpty(extension) && extension.All(c => Char.IsUpper(c)); + return !unprocessed; + } + public string TranslateEnumName(string name) { if (String.IsNullOrEmpty(name)) @@ -147,70 +158,73 @@ namespace Bind if (Utilities.Keywords(Settings.Language).Contains(name)) return name; - if (Char.IsDigit(name[0])) - name = Settings.ConstantPrefix + name; + if (!IsAlreadyProcessed(name)) + { + if (Char.IsDigit(name[0])) + name = Settings.ConstantPrefix + name; - StringBuilder translator = new StringBuilder(name); + StringBuilder translator = new StringBuilder(name); - // Split on IHV names and acronyms, to ensure that characters appearing after these name are uppercase. - var match = Utilities.Acronyms.Match(name); - int offset = 0; // Everytime we insert a match, we must increase offset to compensate. - while (match.Success) - { - int insert_pos = match.Index + match.Length + offset++; - translator.Insert(insert_pos, "_"); - match = match.NextMatch(); - } - name = translator.ToString(); - translator.Remove(0, translator.Length); - - // Process according to these rules: - // 1. if current char is '_', '-' remove it and make next char uppercase - // 2. if current char is or '0-9' keep it and make next char uppercase. - // 3. if current char is uppercase make next char lowercase. - // 4. if current char is lowercase, respect next char case. - bool is_after_underscore_or_number = true; - bool is_previous_uppercase = false; - foreach (char c in name) - { - char char_to_add; - if (c == '_' || c == '-') + // Split on IHV names and acronyms, to ensure that characters appearing after these name are uppercase. + var match = Utilities.Acronyms.Match(name); + int offset = 0; // Everytime we insert a match, we must increase offset to compensate. + while (match.Success) { - is_after_underscore_or_number = true; - continue; // skip this character + int insert_pos = match.Index + match.Length + offset++; + translator.Insert(insert_pos, "_"); + match = match.NextMatch(); } - else if (Char.IsDigit(c)) + name = translator.ToString(); + translator.Remove(0, translator.Length); + + // Process according to these rules: + // 1. if current char is '_', '-' remove it and make next char uppercase + // 2. if current char is or '0-9' keep it and make next char uppercase. + // 3. if current char is uppercase make next char lowercase. + // 4. if current char is lowercase, respect next char case. + bool is_after_underscore_or_number = true; + bool is_previous_uppercase = false; + foreach (char c in name) { - is_after_underscore_or_number = true; - } + char char_to_add; + if (c == '_' || c == '-') + { + is_after_underscore_or_number = true; + continue; // skip this character + } + else if (Char.IsDigit(c)) + { + is_after_underscore_or_number = true; + } - if (is_after_underscore_or_number) - char_to_add = Char.ToUpper(c); - else if (is_previous_uppercase) - char_to_add = Char.ToLower(c); - else - char_to_add = c; + if (is_after_underscore_or_number) + char_to_add = Char.ToUpper(c); + else if (is_previous_uppercase) + char_to_add = Char.ToLower(c); + else + char_to_add = c; - translator.Append(char_to_add); + translator.Append(char_to_add); - is_previous_uppercase = Char.IsUpper(c); - is_after_underscore_or_number = false; - } + is_previous_uppercase = Char.IsUpper(c); + is_after_underscore_or_number = false; + } - // First letter should always be uppercase in order - // to conform to .Net style guidelines. - translator[0] = Char.ToUpper(translator[0]); + // First letter should always be uppercase in order + // to conform to .Net style guidelines. + translator[0] = Char.ToUpper(translator[0]); - // Replace a number of words that do not play well - // with the previous process (i.e. they have two - // consecutive uppercase letters). - translator.Replace("Pname", "PName"); - translator.Replace("AttribIp", "AttribIP"); - translator.Replace("SRgb", "Srgb"); + // Replace a number of words that do not play well + // with the previous process (i.e. they have two + // consecutive uppercase letters). + translator.Replace("Pname", "PName"); + translator.Replace("AttribIp", "AttribIP"); + translator.Replace("SRgb", "Srgb"); - name = translator.ToString(); - if (name.StartsWith(Settings.EnumPrefix)) - name = name.Substring(Settings.EnumPrefix.Length); + name = translator.ToString(); + if (name.StartsWith(Settings.EnumPrefix)) + name = name.Substring(Settings.EnumPrefix.Length); + } return name; } diff --git a/Source/Bind/Utilities.cs b/Source/Bind/Utilities.cs index a8752b7..5df53a4 100644 --- a/Source/Bind/Utilities.cs +++ b/Source/Bind/Utilities.cs @@ -103,31 +103,42 @@ namespace Bind public static readonly char[] Separators = { ' ', '\n', ',', '(', ')', ';', '#' }; public static Regex Extensions { get; private set; } public static Regex Acronyms { get; private set; } - //public static readonly Regex Extensions = new Regex( - // "ARB|EXT|ATIX|ATI|AMDX|AMD|NV|NVX|SUNX|SUN|SGIS|SGIX|SGI|MESAX|MESA|3DFX|IBM|GREMEDY|HP|INTEL|PGI|INGR|APPLE|OML|I3D|ARM|ANGLE|OES|QCOM|VIV|IMG", - // RegexOptions.Compiled); - //public static readonly Regex Acronyms = new Regex(Extensions.ToString() + "|EGL|3TC|DXT|ES|GL|CL|RGBA|BGRA|RGB|BGR|ETC", - // RegexOptions.Compiled); - public static void InitExtensions(IEnumerable extensions) + static List extension_names = new List(); + + public static void AddExtensions(IEnumerable extensions) { - var acronyms = new string[] + // Merge the new extensions with the current list of extensions + int extension_count = extension_names.Count; + extension_names.AddRange( + extensions.Where(n => !extension_names.Contains(n))); + + // If any new extensions have been added, + // recreate the Extensions regex. + if (extension_names.Count != extension_count) { - "EGL", "ES", "GL", "CL", - "RGBA", "BGRA", "RGB", "BGR", - "SRGB", "YCBCR", - "3TC", "DXT", "BPTC", "RGTC", - "3DC", "ATC", "ETC", - "ANGLE", "MESAX", "MESA", - }; - - Extensions = new Regex( - String.Join("|", extensions.ToArray()), - RegexOptions.Compiled); - - Acronyms = new Regex( - String.Join("|", extensions.Concat(acronyms).ToArray()), - RegexOptions.Compiled); + // Sort longest extensions first, otherwise SGIS may be + // incorrectly matched as SGI. + extension_names.Sort((a, b) => b.Length.CompareTo(a.Length)); + + Extensions = new Regex( + String.Join("|", extension_names.ToArray()), + RegexOptions.Compiled); + + var acronyms = new string[] + { + "EGL", "ES", "GL", "CL", + "RGBA", "BGRA", "RGB", "BGR", + "SRGB", "YCBCR", + "3TC", "DXT", "BPTC", "RGTC", + "3DC", "ATC", "ETC", + "ANGLE", "MESAX", "MESA", + }; + + var acronym_names = extensions.Concat(acronyms).ToList(); + acronym_names.Sort((a, b) => b.Length.CompareTo(a.Length)); + Acronyms = new Regex(String.Join("|", acronym_names.ToArray()), RegexOptions.Compiled); + } } #region internal StreamReader OpenSpecFile(string file) @@ -272,13 +283,21 @@ namespace Bind internal static string GetGL2Extension(string name) { + return GetExtension(name, false); + } + + internal static string GetExtension(string name, bool return_unmodified) + { var match = Extensions.Match(name); if (match.Success) { string ext = match.Value; - if (ext.Length > 2) + if (!return_unmodified) { - ext = ext[0] + ext.Substring(1).ToLower(); + if (ext.Length > 2) + { + ext = ext[0] + ext.Substring(1).ToLower(); + } } return ext; } diff --git a/Source/Bind/XmlSpecReader.cs b/Source/Bind/XmlSpecReader.cs index 6bb8a17..bc16062 100644 --- a/Source/Bind/XmlSpecReader.cs +++ b/Source/Bind/XmlSpecReader.cs @@ -331,7 +331,7 @@ namespace Bind delegates.Add(d); } - Utilities.InitExtensions(extensions); + Utilities.AddExtensions(extensions); return delegates; } -- 2.7.4