[mono-api-info] Rework AttributeData to use a static method instead of creating an...
[mono.git] / mcs / tools / corcompare / mono-api-info.cs
index 4abfffd894633b8951ed31e7fd5899c980793e00..a76a422458c0c8263ba6846777c5f93feb33a199 100644 (file)
@@ -119,6 +119,7 @@ namespace CorCompare
        }
 
        public class Utils {
+               static char[] CharsToCleanup = new char[] { '<', '>', '/' };
 
                public static string CleanupTypeName (TypeReference type)
                {
@@ -127,7 +128,27 @@ namespace CorCompare
 
                public static string CleanupTypeName (string t)
                {
-                       return t.Replace ('<', '[').Replace ('>', ']').Replace ('/', '+');
+                       if (t.IndexOfAny (CharsToCleanup) == -1)
+                               return t;
+                       var sb = new StringBuilder (t.Length);
+                       for (int i = 0; i < t.Length; i++) {
+                               var ch = t [i];
+                               switch (ch) {
+                               case '<':
+                                       sb.Append ('[');
+                                       break;
+                               case '>':
+                                       sb.Append (']');
+                                       break;
+                               case '/':
+                                       sb.Append ('+');
+                                       break;
+                               default:
+                                       sb.Append (ch);
+                                       break;
+                               }
+                       }
+                       return sb.ToString ();
                }
        }
 
@@ -1085,17 +1106,9 @@ namespace CorCompare
                }
        }
 
-       class AttributeData : BaseData
+       class AttributeData
        {
-               IList<ICustomAttributeProvider> providers;
-
-               AttributeData (XmlWriter writer, IList<ICustomAttributeProvider> providers)
-                       : base (writer)
-               {
-                       this.providers = providers;
-               }
-
-               public override void DoOutput ()
+               public static void DoOutput (XmlWriter writer, IList<ICustomAttributeProvider> providers)
                {
                        if (writer == null)
                                throw new InvalidOperationException ("Document not set");
@@ -1120,13 +1133,15 @@ namespace CorCompare
                                if (ass != null && !Driver.FollowForwarders)
                                        TypeForwardedToData.OutputForwarders (writer, ass);
 
-                               foreach (var att in provider.CustomAttributes.OrderBy ((a) => a.Constructor.DeclaringType.FullName)) {
+                               var attributes = provider.CustomAttributes.
+                                       Where ((att) => !SkipAttribute (att)).
+                                       OrderBy ((a) => a.Constructor.DeclaringType.FullName, StringComparer.Ordinal);
+                               
+                               foreach (var att in attributes) {
                                        string attName = Utils.CleanupTypeName (att.Constructor.DeclaringType);
-                                       if (SkipAttribute (att))
-                                               continue;
 
                                        writer.WriteStartElement ("attribute");
-                                       AddAttribute ("name", attName);
+                                       writer.WriteAttributeString ("name", attName);
 
                                        var attribute_mapping = CreateAttributeMapping (att).Where ((kvp) => kvp.Key != "TypeId");
 
@@ -1137,15 +1152,15 @@ namespace CorCompare
                                                        object o = kvp.Value;
 
                                                        writer.WriteStartElement ("property");
-                                                       AddAttribute ("name", name);
+                                                       writer.WriteAttributeString ("name", name);
 
                                                        if (o == null) {
-                                                               AddAttribute ("value", "null");
+                                                               writer.WriteAttributeString ("value", "null");
                                                        } else {
                                                                string value = o.ToString ();
                                                                if (attName.EndsWith ("GuidAttribute", StringComparison.Ordinal))
                                                                        value = value.ToUpper ();
-                                                               AddAttribute ("value", value);
+                                                               writer.WriteAttributeString ("value", value);
                                                        }
 
                                                        writer.WriteEndElement (); // property
@@ -1413,8 +1428,7 @@ namespace CorCompare
 
                public static void OutputAttributes (XmlWriter writer, params ICustomAttributeProvider[] providers)
                {
-                       AttributeData ad = new AttributeData (writer, providers);
-                       ad.DoOutput ();
+                       AttributeData.DoOutput (writer, providers);
                }
        }