SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
bret.lowery
bret.lowery
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 190
Comments posted to this topic are about the item URI Parser: A set of 20 CLR functions for URI parsing
richardd
richardd
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3240 Visits: 648
Some suggestions: :-)

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


URI.ToString().TrimStart().TrimEnd().Length < 1
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.


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


catch (Exception ex)
Can your code handle every possible error condition? Thread abort? Out of memory? Stack overflow?

No, I didn't think so. :-P

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.


throw ex;
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.


try { ... new Uri(value) ... } catch ...
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);
}





Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search