URI Parser: A set of 20 CLR functions for URI parsing

  • bret.lowery

    Old Hand

    Points: 302

    Comments posted to this topic are about the item URI Parser: A set of 20 CLR functions for URI parsing

  • richardd

    Hall of Fame

    Points: 3899

    Some suggestions: 🙂

    [font="Courier New"]if (URI.IsNull == true)[/font]

    The IsNull property is a boolean, so this should be: "if (URI.IsNull)".

    [font="Courier New"]URI.ToString().TrimStart().TrimEnd().Length < 1[/font]

    Or, as it's known in .NET, ".ToString().Trim().Length == 0". You've just saved one string allocation per call, and you'll never be able to produce a .NET string with a negative length.

    [font="Courier New"]throw new Exception(...)[/font]

    Don't do this. Ever. Throw a more specific exception - ArgumentNullException or ArgumentException would be appropriate here.

    [font="Courier New"]catch (Exception ex)[/font]

    Can your code handle every possible error condition? Thread abort? Out of memory? Stack overflow?

    No, I didn't think so. 😛

    The Uri constructor will only throw two types of exception: ArgumentNullException if the parameter is null, or UriFormatException if the parameter is not a valid URI. You've already tested for a null parameter, so you only need to catch a UriFormatException.

    [font="Courier New"]throw ex;[/font]

    You've just destroyed the stack trace for that exception. Thanks a lot!

    If you need to re-throw an exception, use "throw;", not "throw ex;". Your users will thank you for it.

    [font="Courier New"]try { ... new Uri(value) ... } catch ...[/font]

    If you're going to re-throw the exception, you don't need the "try ... catch" block. If you're not going to re-throw the exception, it would be significantly more efficient to use the static TryCreate method instead.

    Here's a cleaned-up version of the ParseURI function to get you going:

    public static SqlString ParseURI(

    SqlString URI,

    UriComponents Component,

    short EscapeMode,

    SqlBoolean ReturnNullOnParseError,

    SqlBoolean ReturnNullIfURINull,

    SqlBoolean ReturnNullIfURIEmpty

    )

    {

    if (URI.IsNull)

    {

    if (ReturnNullIfURINull.IsTrue) return SqlString.Null;

    throw new ArgumentNullException("URI");

    }

    string uriString = URI.ToString().Trim();

    if (uriString.Length == 0)

    {

    if (ReturnNullIfURIEmpty.IsTrue) return SqlString.Null;

    throw new ArgumentException("URI must not be empty.", "URI");

    }

    Uri uri;

    if (ReturnNullOnParseError.IsTrue)

    {

    if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri))

    {

    return SqlString.Null;

    }

    }

    else

    {

    uri = new Uri(uriString);

    }

    UriFormat format;

    switch (EscapeMode)

    {

    case 1:

    format = UriFormat.Unescaped;

    break;

    case 2:

    format = UriFormat.UriEscaped;

    break;

    default:

    format = UriFormat.SafeUnescaped;

    break;

    }

    return (SqlString)uri.GetComponents(Component, format);

    }

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply