Avoid translating names that do not need translation (i.e. are not in ALL_CAPS).

This commit is contained in:
the_fiddler 2009-10-14 22:49:04 +00:00
parent 4abe4416a8
commit eb2d1c3c91
2 changed files with 14 additions and 7 deletions

View file

@ -7,6 +7,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Text; using System.Text;
namespace Bind.Structures namespace Bind.Structures
@ -174,7 +175,10 @@ namespace Bind.Structures
translator.Remove(0, translator.Length); translator.Remove(0, translator.Length);
// Translate the constant's name to match .Net naming conventions // Translate the constant's name to match .Net naming conventions
if ((Settings.Compatibility & Settings.Legacy.NoAdvancedEnumProcessing) == Settings.Legacy.None) bool name_is_all_caps = s.AsEnumerable().All(c => Char.IsLetter(c) ? Char.IsUpper(c) : true);
bool name_contains_underscore = s.Contains("_");
if ((Settings.Compatibility & Settings.Legacy.NoAdvancedEnumProcessing) == Settings.Legacy.None &&
(name_is_all_caps || name_contains_underscore))
{ {
bool next_char_uppercase = true; bool next_char_uppercase = true;
bool is_after_digit = false; bool is_after_digit = false;

View file

@ -7,6 +7,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Xml.XPath; using System.Xml.XPath;
@ -118,6 +119,7 @@ namespace Bind.Structures
#region TranslateName #region TranslateName
// Translate the constant's name to match .Net naming conventions
public static string TranslateName(string name) public static string TranslateName(string name)
{ {
if (String.IsNullOrEmpty(name)) if (String.IsNullOrEmpty(name))
@ -128,13 +130,12 @@ namespace Bind.Structures
translator.Remove(0, translator.Length); // Trick to avoid allocating a new StringBuilder. translator.Remove(0, translator.Length); // Trick to avoid allocating a new StringBuilder.
// Translate the constant's name to match .Net naming conventions
if ((Settings.Compatibility & Settings.Legacy.NoAdvancedEnumProcessing) == Settings.Legacy.None) if ((Settings.Compatibility & Settings.Legacy.NoAdvancedEnumProcessing) == Settings.Legacy.None)
{ {
bool is_after_underscore_or_number = true; // Detect if we just passed a '_' or a number and make the next char // Detect if we just passed a '_' or a number and make the next char uppercase.
// uppercase. bool is_after_underscore_or_number = true;
bool is_previous_uppercase = false; // Detect if previous character was uppercase, and turn // Detect if previous character was uppercase, and turn the current one to lowercase.
// the current one to lowercase. bool is_previous_uppercase = false;
foreach (char c in name) foreach (char c in name)
{ {
@ -145,10 +146,12 @@ namespace Bind.Structures
{ {
if (Char.IsDigit(c)) if (Char.IsDigit(c))
is_after_underscore_or_number = true; is_after_underscore_or_number = true;
char_to_add = is_after_underscore_or_number ? Char.ToUpper(c) : char_to_add = is_after_underscore_or_number ? Char.ToUpper(c) :
is_previous_uppercase ? Char.ToLower(c) : c; is_previous_uppercase ? Char.ToLower(c) : c;
is_previous_uppercase = Char.IsUpper(c);
translator.Append(char_to_add); translator.Append(char_to_add);
is_previous_uppercase = Char.IsUpper(c);
is_after_underscore_or_number = false; is_after_underscore_or_number = false;
} }
} }