[Bind] Do not re-translate translated enum names

This commit is contained in:
thefiddler 2014-08-14 10:30:14 +02:00
parent d50bef3ba6
commit 5ec28b65be
3 changed files with 112 additions and 79 deletions

View file

@ -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,71 +158,74 @@ namespace Bind
if (Utilities.Keywords(Settings.Language).Contains(name))
return name;
if (Char.IsDigit(name[0]))
name = Settings.ConstantPrefix + 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)
if (!IsAlreadyProcessed(name))
{
int insert_pos = match.Index + match.Length + offset++;
translator.Insert(insert_pos, "_");
match = match.NextMatch();
}
name = translator.ToString();
translator.Remove(0, translator.Length);
if (Char.IsDigit(name[0]))
name = Settings.ConstantPrefix + name;
// 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 == '-')
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)
{
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;
translator.Append(char_to_add);
is_previous_uppercase = Char.IsUpper(c);
is_after_underscore_or_number = false;
}
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;
// First letter should always be uppercase in order
// to conform to .Net style guidelines.
translator[0] = Char.ToUpper(translator[0]);
translator.Append(char_to_add);
// 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");
is_previous_uppercase = Char.IsUpper(c);
is_after_underscore_or_number = false;
name = translator.ToString();
if (name.StartsWith(Settings.EnumPrefix))
name = name.Substring(Settings.EnumPrefix.Length);
}
// 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");
name = translator.ToString();
if (name.StartsWith(Settings.EnumPrefix))
name = name.Substring(Settings.EnumPrefix.Length);
return name;
}

View file

@ -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<string> extensions)
static List<string> extension_names = new List<string>();
public static void AddExtensions(IEnumerable<string> extensions)
{
var acronyms = new string[]
{
"EGL", "ES", "GL", "CL",
"RGBA", "BGRA", "RGB", "BGR",
"SRGB", "YCBCR",
"3TC", "DXT", "BPTC", "RGTC",
"3DC", "ATC", "ETC",
"ANGLE", "MESAX", "MESA",
};
// 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)));
Extensions = new Regex(
String.Join("|", extensions.ToArray()),
RegexOptions.Compiled);
Acronyms = new Regex(
String.Join("|", extensions.Concat(acronyms).ToArray()),
RegexOptions.Compiled);
// If any new extensions have been added,
// recreate the Extensions regex.
if (extension_names.Count != extension_count)
{
// 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)
@ -271,14 +282,22 @@ namespace Bind
#region internal static string GetGL2Extension(string name)
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;
}

View file

@ -331,7 +331,7 @@ namespace Bind
delegates.Add(d);
}
Utilities.InitExtensions(extensions);
Utilities.AddExtensions(extensions);
return delegates;
}