< Summary

Information
Class: Ice.Util
Assembly: Ice
File(s): /_/csharp/src/Ice/Util.cs
Tag: 125_37167941578
Line coverage
67%
Covered lines: 61
Uncovered lines: 29
Coverable lines: 90
Total lines: 311
Line coverage: 67.7%
Branch coverage
56%
Covered branches: 33
Total branches: 58
Branch coverage: 56.8%
Method coverage
57%
Covered methods: 8
Fully covered methods: 5
Total methods: 14
Method coverage: 57.1%
Full method coverage: 35.7%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
createProperties()100%210%
createProperties(...)100%210%
initialize(...)100%11100%
initialize(...)100%210%
stringToIdentity(...)85.71%161478.57%
identityToString(...)100%22100%
generateUUID()100%210%
proxyIdentityCompare(...)42.86%211466.67%
proxyIdentityAndFacetCompare(...)45.83%402469.57%
getProcessLogger()100%22100%
setProcessLogger(...)0%620%
stringVersion()100%210%
intVersion()100%11100%
.cctor()100%11100%

File(s)

/_/csharp/src/Ice/Util.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3#nullable enable
 4
 5namespace Ice;
 6
 7/// <summary>
 8/// Utility methods for the Ice runtime.
 9/// </summary>
 10public sealed class Util
 11{
 12    /// <summary>
 13    /// Creates a new empty property set.
 14    /// </summary>
 15    /// <returns>A new empty property set.</returns>
 16    /// <remarks>This method is provided for backwards compatibility. New code should call the
 17    /// <see cref="Properties()" /> constructor directly.</remarks>
 018    public static Properties createProperties() => new();
 19
 20    /// <summary>
 21    /// Creates a property set initialized from command-line arguments and a default property set.
 22    /// </summary>
 23    /// <param name="args">The command-line arguments.</param>
 24    /// <param name="defaults">Default values for the new property set.</param>
 25    /// <returns>A new property set.</returns>
 26    /// <remarks>This method is provided for backwards compatibility. New code should call the
 27    /// <see cref="Properties(ref string[], Properties?)" /> constructor directly.</remarks>
 28    public static Properties createProperties(ref string[] args, Properties? defaults = null) =>
 029        new(ref args, defaults);
 30
 31    /// <summary>
 32    /// Creates a new communicator.
 33    /// </summary>
 34    /// <param name="initData">Options for the new communicator.</param>
 35    /// <returns>The new communicator.</returns>
 36    /// <remarks>This method is provided for backwards compatibility. New code should call the
 37    /// <see cref="Communicator(InitializationData?)" /> constructor directly.</remarks>
 138    public static Communicator initialize(InitializationData? initData = null) => new(initData);
 39
 40    /// <summary>
 41    /// Creates a new communicator, using Ice properties parsed from command-line arguments.
 42    /// </summary>
 43    /// <param name="args">Command-line arguments.</param>
 44    /// <returns>The new communicator.</returns>
 45    /// <remarks>This method is provided for backwards compatibility. New code should call the
 46    /// <see cref="Communicator(ref string[])" /> constructor directly.</remarks>
 047    public static Communicator initialize(ref string[] args) => new(ref args);
 48
 49    /// <summary>
 50    /// Converts a string to an object identity.
 51    /// </summary>
 52    /// <param name="s">The string to convert.</param>
 53    /// <returns>The converted object identity.</returns>
 54    public static Identity stringToIdentity(string s)
 55    {
 156        var ident = new Identity();
 57
 58        //
 59        // Find unescaped separator; note that the string may contain an escaped
 60        // backslash before the separator.
 61        //
 162        int slash = -1, pos = 0;
 163        while ((pos = s.IndexOf('/', pos)) != -1)
 64        {
 165            int escapes = 0;
 166            while (pos - escapes > 0 && s[pos - escapes - 1] == '\\')
 67            {
 168                escapes++;
 69            }
 70
 71            //
 72            // We ignore escaped escapes
 73            //
 174            if (escapes % 2 == 0)
 75            {
 176                if (slash == -1)
 77                {
 178                    slash = pos;
 79                }
 80                else
 81                {
 82                    //
 83                    // Extra unescaped slash found.
 84                    //
 085                    throw new ParseException($"unescaped slash in identity string '{s}'");
 86                }
 87            }
 188            pos++;
 89        }
 90
 191        if (slash == -1)
 92        {
 193            ident.category = "";
 94            try
 95            {
 196                ident.name = Ice.UtilInternal.StringUtil.unescapeString(s, 0, s.Length, "/");
 197            }
 198            catch (ArgumentException e)
 99            {
 1100                throw new ParseException($"invalid name in identity string '{s}'", e);
 101            }
 102        }
 103        else
 104        {
 105            try
 106            {
 1107                ident.category = Ice.UtilInternal.StringUtil.unescapeString(s, 0, slash, "/");
 1108            }
 0109            catch (ArgumentException e)
 110            {
 0111                throw new ParseException($"invalid category in identity string '{s}'", e);
 112            }
 1113            if (slash + 1 < s.Length)
 114            {
 115                try
 116                {
 1117                    ident.name = Ice.UtilInternal.StringUtil.unescapeString(s, slash + 1, s.Length, "/");
 1118                }
 0119                catch (ArgumentException e)
 120                {
 0121                    throw new ParseException($"invalid name in identity string '{s}'", e);
 122                }
 123            }
 124            else
 125            {
 0126                ident.name = "";
 127            }
 128        }
 129
 1130        return ident;
 131    }
 132
 133    /// <summary>
 134    /// Converts an object identity to a string.
 135    /// </summary>
 136    /// <param name="ident">The object identity to convert.</param>
 137    /// <param name="toStringMode">Specifies how to handle non-ASCII characters and non-printable ASCII
 138    /// characters.</param>
 139    /// <returns>The string representation of the object identity.</returns>
 140    public static string identityToString(Identity ident, ToStringMode toStringMode = ToStringMode.Unicode)
 141    {
 1142        if (ident.category.Length == 0)
 143        {
 1144            return Ice.UtilInternal.StringUtil.escapeString(ident.name, "/", toStringMode);
 145        }
 146        else
 147        {
 1148            return Ice.UtilInternal.StringUtil.escapeString(ident.category, "/", toStringMode) + '/' +
 1149                Ice.UtilInternal.StringUtil.escapeString(ident.name, "/", toStringMode);
 150        }
 151    }
 152
 153    /// <summary>
 154    /// This method is deprecated. Use System.Guid instead.
 155    /// </summary>
 156    /// <returns>A new UUID.</returns>
 157    [Obsolete("This method is deprecated. Use System.Guid instead.")]
 158    public static string generateUUID() =>
 0159        Guid.NewGuid().ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture);
 160
 161    /// <summary>
 162    /// Compares the object identities of two proxies.
 163    /// </summary>
 164    /// <param name="lhs">The first proxy to compare.</param>
 165    /// <param name="rhs">The second proxy to compare.</param>
 166    /// <returns>-1 if the identity in lhs compares less than the identity in rhs; 0 if the identities compare equal;
 167    /// otherwise, 1.</returns>
 168    public static int proxyIdentityCompare(ObjectPrx? lhs, ObjectPrx? rhs)
 169    {
 1170        if (lhs == null && rhs == null)
 171        {
 0172            return 0;
 173        }
 1174        else if (lhs == null && rhs != null)
 175        {
 0176            return -1;
 177        }
 1178        else if (lhs != null && rhs == null)
 179        {
 0180            return 1;
 181        }
 182        else
 183        {
 1184            Identity lhsIdentity = lhs!.ice_getIdentity();
 1185            Identity rhsIdentity = rhs!.ice_getIdentity();
 186            int n;
 1187            n = string.CompareOrdinal(lhsIdentity.name, rhsIdentity.name);
 1188            if (n != 0)
 189            {
 0190                return n;
 191            }
 1192            return string.CompareOrdinal(lhsIdentity.category, rhsIdentity.category);
 193        }
 194    }
 195
 196    /// <summary>
 197    /// Compares the object identities and facets of two proxies.
 198    /// </summary>
 199    /// <param name="lhs">The first proxy to compare.</param>
 200    /// <param name="rhs">The second proxy to compare.</param>
 201    /// <returns>-1 if the identity and facet in lhs compare
 202    /// less than the identity and facet in rhs; 0 if the identities
 203    /// and facets compare equal; otherwise, 1.</returns>
 204    public static int proxyIdentityAndFacetCompare(ObjectPrx? lhs, ObjectPrx? rhs)
 205    {
 1206        if (lhs == null && rhs == null)
 207        {
 0208            return 0;
 209        }
 1210        else if (lhs == null && rhs != null)
 211        {
 0212            return -1;
 213        }
 1214        else if (lhs != null && rhs == null)
 215        {
 0216            return 1;
 217        }
 218        else
 219        {
 1220            Identity lhsIdentity = lhs!.ice_getIdentity();
 1221            Identity rhsIdentity = rhs!.ice_getIdentity();
 222            int n;
 1223            n = string.CompareOrdinal(lhsIdentity.name, rhsIdentity.name);
 1224            if (n != 0)
 225            {
 1226                return n;
 227            }
 1228            n = string.CompareOrdinal(lhsIdentity.category, rhsIdentity.category);
 1229            if (n != 0)
 230            {
 0231                return n;
 232            }
 233
 1234            string lhsFacet = lhs.ice_getFacet();
 1235            string rhsFacet = rhs.ice_getFacet();
 1236            if (lhsFacet == null && rhsFacet == null)
 237            {
 0238                return 0;
 239            }
 1240            else if (lhsFacet == null)
 241            {
 0242                return -1;
 243            }
 1244            else if (rhsFacet == null)
 245            {
 0246                return 1;
 247            }
 248            else
 249            {
 1250                return string.CompareOrdinal(lhsFacet, rhsFacet);
 251            }
 252        }
 253    }
 254
 255    /// <summary>
 256    /// Returns the process-wide logger.
 257    /// </summary>
 258    /// <returns>The process-wide logger.</returns>
 259    public static Logger getProcessLogger()
 260    {
 1261        lock (_processLoggerMutex)
 262        {
 1263            if (_processLogger is null)
 264            {
 1265                _processLogger = new ConsoleLoggerI(AppDomain.CurrentDomain.FriendlyName);
 1266                _ownProcessLogger = true;
 267            }
 1268            return _processLogger;
 269        }
 1270    }
 271
 272    /// <summary>
 273    /// Changes the process-wide logger.
 274    /// </summary>
 275    /// <param name="logger">The new process-wide logger.</param>
 276    public static void setProcessLogger(Logger logger)
 277    {
 0278        lock (_processLoggerMutex)
 279        {
 0280            if (_ownProcessLogger)
 281            {
 0282                _processLogger!.Dispose();
 283            }
 0284            _processLogger = logger;
 0285            _ownProcessLogger = false;
 0286        }
 0287    }
 288
 289    /// <summary>
 290    /// Returns the Ice version in the form A.B.C, where A indicates the
 291    /// major version, B indicates the minor version, and C indicates the
 292    /// patch level.
 293    /// </summary>
 294    /// <returns>The Ice version.</returns>
 0295    public static string stringVersion() => "3.9.0-alpha.0"; // "A.B.C", with A=major, B=minor, C=patch
 296
 297    /// <summary>
 298    /// Returns the Ice version as an integer in the form AABBCC, where AA
 299    /// indicates the major version, BB indicates the minor version, and CC
 300    /// indicates the patch level. For example, for Ice 3.9.1, the returned value is 30901.
 301    /// </summary>
 302    /// <returns>The Ice version.</returns>
 1303    public static int intVersion() => 30950; // AABBCC, with AA=major, BB=minor, CC=patch
 304
 1305    public static readonly EncodingVersion Encoding_1_0 = new(1, 0);
 1306    public static readonly EncodingVersion Encoding_1_1 = new(1, 1);
 307
 1308    private static readonly object _processLoggerMutex = new object();
 309    private static Logger? _processLogger;
 310    private static bool _ownProcessLogger;
 311}