From 0527568d0400219bc130f319a017dcc661eaf942 Mon Sep 17 00:00:00 2001 From: sta Date: Tue, 14 Jan 2014 03:29:43 +0900 Subject: [PATCH] Refactored Ext.cs --- websocket-sharp/Ext.cs | 593 ++++++++++++++++++++++------------------- 1 file changed, 320 insertions(+), 273 deletions(-) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index aad7adc2..4396dc08 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -15,7 +15,7 @@ * Copyright (c) 2003 Ben Maurer * Copyright (c) 2003, 2005, 2009 Novell, Inc. (http://www.novell.com) * Copyright (c) 2009 Stephane Delcroix - * Copyright (c) 2010-2013 sta.blockhead + * Copyright (c) 2010-2014 sta.blockhead * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -71,8 +71,7 @@ namespace WebSocketSharp //return new Byte [] { 0x00, 0x00, 0x00, 0xff, 0xff }; return value; - using (var input = new MemoryStream (value)) - { + using (var input = new MemoryStream (value)) { return input.compressToArray (); } } @@ -84,8 +83,7 @@ namespace WebSocketSharp return output; stream.Position = 0; - using (var ds = new DeflateStream (output, CompressionMode.Compress, true)) - { + using (var ds = new DeflateStream (output, CompressionMode.Compress, true)) { stream.CopyTo (ds); ds.Close (); // "BFINAL" set to 1. output.Position = 0; @@ -96,8 +94,7 @@ namespace WebSocketSharp private static byte [] compressToArray (this Stream stream) { - using (var comp = stream.compress ()) - { + using (var comp = stream.compress ()) { comp.Close (); return comp.ToArray (); } @@ -108,8 +105,7 @@ namespace WebSocketSharp if (value.LongLength == 0) return value; - using (var input = new MemoryStream (value)) - { + using (var input = new MemoryStream (value)) { return input.decompressToArray (); } } @@ -121,8 +117,7 @@ namespace WebSocketSharp return output; stream.Position = 0; - using (var ds = new DeflateStream (stream, CompressionMode.Decompress, true)) - { + using (var ds = new DeflateStream (stream, CompressionMode.Decompress, true)) { ds.CopyTo (output, true); return output; } @@ -130,22 +125,21 @@ namespace WebSocketSharp private static byte [] decompressToArray (this Stream stream) { - using (var decomp = stream.decompress ()) - { + using (var decomp = stream.decompress ()) { decomp.Close (); return decomp.ToArray (); } } - private static byte [] readBytes (this Stream stream, byte [] buffer, int offset, int length) + private static byte [] readBytes ( + this Stream stream, byte [] buffer, int offset, int length) { var len = stream.Read (buffer, offset, length); if (len < 1) return buffer.SubArray (0, offset); var tmp = 0; - while (len < length) - { + while (len < length) { tmp = stream.Read (buffer, offset + len, length - len); if (tmp < 1) break; @@ -303,7 +297,8 @@ namespace WebSocketSharp response.Close (HttpStatusCode.Unauthorized); } - internal static byte [] Compress (this byte [] value, CompressionMethod method) + internal static byte [] Compress ( + this byte [] value, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? value.compress () @@ -317,7 +312,8 @@ namespace WebSocketSharp : stream; } - internal static byte [] CompressToArray (this Stream stream, CompressionMethod method) + internal static byte [] CompressToArray ( + this Stream stream, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? stream.compressToArray () @@ -337,13 +333,13 @@ namespace WebSocketSharp src.CopyTo (dest, false); } - internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition) + internal static void CopyTo ( + this Stream src, Stream dest, bool setDefaultPosition) { var readLen = 0; var bufferLen = 256; var buffer = new byte [bufferLen]; - while ((readLen = src.Read (buffer, 0, bufferLen)) > 0) - { + while ((readLen = src.Read (buffer, 0, bufferLen)) > 0) { dest.Write (buffer, 0, readLen); } @@ -351,21 +347,24 @@ namespace WebSocketSharp dest.Position = 0; } - internal static byte [] Decompress (this byte [] value, CompressionMethod method) + internal static byte [] Decompress ( + this byte [] value, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? value.decompress () : value; } - internal static Stream Decompress (this Stream stream, CompressionMethod method) + internal static Stream Decompress ( + this Stream stream, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? stream.decompress () : stream; } - internal static byte [] DecompressToArray (this Stream stream, CompressionMethod method) + internal static byte [] DecompressToArray ( + this Stream stream, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? stream.decompressToArray () @@ -378,11 +377,13 @@ namespace WebSocketSharp } /// - /// Determines whether the specified equals the specified , - /// and invokes the specified Action<int> delegate at the same time. + /// Determines whether the specified equals the specified + /// , and invokes the specified Action<int> delegate + /// at the same time. /// /// - /// true if equals ; otherwise, false. + /// true if equals ; + /// otherwise, false. /// /// /// An to compare. @@ -391,11 +392,12 @@ namespace WebSocketSharp /// A to compare. /// /// - /// An Action<int> delegate that references the method(s) called at the same time as comparing. - /// An parameter to pass to the method(s) is . + /// An Action<int> delegate that references the method(s) called at the + /// same time as comparing. An parameter to pass to the + /// method(s) is . /// /// - /// is not between 0 and 255. + /// isn't between 0 and 255. /// internal static bool EqualsWith (this int value, char c, Action action) { @@ -410,11 +412,11 @@ namespace WebSocketSharp /// Gets the absolute path from the specified . /// /// - /// A that contains the absolute path if it is successfully found; - /// otherwise, . + /// A that represents the absolute path if it's + /// successfully found; otherwise, . /// /// - /// A that contains a URI to get the absolute path from. + /// A that represents a URI to get the absolute path from. /// internal static string GetAbsolutePath (this Uri uri) { @@ -454,7 +456,8 @@ namespace WebSocketSharp : String.Empty; } - internal static string GetNameInternal (this string nameAndValue, string separator) + internal static string GetNameInternal ( + this string nameAndValue, string separator) { int i = nameAndValue.IndexOf (separator); return i > 0 @@ -462,7 +465,8 @@ namespace WebSocketSharp : null; } - internal static string GetValueInternal (this string nameAndValue, string separator) + internal static string GetValueInternal ( + this string nameAndValue, string separator) { int i = nameAndValue.IndexOf (separator); return i >= 0 && i < nameAndValue.Length - 1 @@ -505,8 +509,7 @@ namespace WebSocketSharp internal static bool IsText (this string value) { int len = value.Length; - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { char c = value [i]; if (c < 0x20 && !"\r\n\t".Contains (c)) return false; @@ -514,8 +517,7 @@ namespace WebSocketSharp if (c == 0x7f) return false; - if (c == '\n' && ++i < len) - { + if (c == '\n' && ++i < len) { c = value [i]; if (!" \t".Contains (c)) return false; @@ -597,19 +599,17 @@ namespace WebSocketSharp return stream.readBytes (new byte [length], 0, length); } - internal static byte [] ReadBytes (this Stream stream, long length, int bufferLength) + internal static byte [] ReadBytes ( + this Stream stream, long length, int bufferLength) { - using (var result = new MemoryStream ()) - { + using (var result = new MemoryStream ()) { var count = length / bufferLength; var rem = (int) (length % bufferLength); var buffer = new byte [bufferLength]; var end = false; - for (long i = 0; i < count; i++) - { - if (!stream.readBytes (buffer, 0, bufferLength, result)) - { + for (long i = 0; i < count; i++) { + if (!stream.readBytes (buffer, 0, bufferLength, result)) { end = true; break; } @@ -624,38 +624,42 @@ namespace WebSocketSharp } internal static void ReadBytesAsync ( - this Stream stream, int length, Action completed, Action error) + this Stream stream, + int length, + Action completed, + Action error) { var buffer = new byte [length]; - AsyncCallback callback = ar => - { - try { - var len = stream.EndRead (ar); - var bytes = len < 1 - ? new byte []{} - : len < length - ? stream.readBytes (buffer, len, length - len) - : buffer; - - if (completed != null) - completed (bytes); - } - catch (Exception ex) { - if (error != null) - error (ex); - } - }; - - stream.BeginRead (buffer, 0, length, callback, null); + stream.BeginRead ( + buffer, + 0, + length, + ar => { + try { + var len = stream.EndRead (ar); + var bytes = len < 1 + ? new byte [0] + : len < length + ? stream.readBytes (buffer, len, length - len) + : buffer; + + if (completed != null) + completed (bytes); + } + catch (Exception ex) { + if (error != null) + error (ex); + } + }, + null); } - internal static string RemovePrefix (this string value, params string [] prefixes) + internal static string RemovePrefix ( + this string value, params string [] prefixes) { int i = 0; - foreach (var prefix in prefixes) - { - if (value.StartsWith (prefix)) - { + foreach (var prefix in prefixes) { + if (value.StartsWith (prefix)) { i = prefix.Length; break; } @@ -666,32 +670,28 @@ namespace WebSocketSharp : value; } - internal static IEnumerable SplitHeaderValue (this string value, params char [] separator) + internal static IEnumerable SplitHeaderValue ( + this string value, params char [] separator) { var separators = new string (separator); var buffer = new StringBuilder (64); int len = value.Length; bool quoted = false; bool escaped = false; - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { char c = value [i]; - if (c == '"') - { + if (c == '"') { if (escaped) escaped = !escaped; else quoted = !quoted; } - else if (c == '\\') - { + else if (c == '\\') { if (i < len - 1 && value [i + 1] == '"') escaped = true; } - else if (separators.Contains (c)) - { - if (!quoted) - { + else if (separators.Contains (c)) { + if (!quoted) { yield return buffer.ToString (); buffer.Length = 0; continue; @@ -709,8 +709,7 @@ namespace WebSocketSharp internal static byte [] ToByteArray (this Stream stream) { - using (var output = new MemoryStream ()) - { + using (var output = new MemoryStream ()) { stream.Position = 0; stream.CopyTo (output); output.Close (); @@ -719,7 +718,8 @@ namespace WebSocketSharp } } - internal static byte [] ToByteArrayInternally (this ushort value, ByteOrder order) + internal static byte [] ToByteArrayInternally ( + this ushort value, ByteOrder order) { var buffer = BitConverter.GetBytes (value); return order.IsHostOrder () @@ -727,7 +727,8 @@ namespace WebSocketSharp : buffer.Reverse ().ToArray (); } - internal static byte [] ToByteArrayInternally (this ulong value, ByteOrder order) + internal static byte [] ToByteArrayInternally ( + this ulong value, ByteOrder order) { var buffer = BitConverter.GetBytes (value); return order.IsHostOrder () @@ -751,7 +752,8 @@ namespace WebSocketSharp return CompressionMethod.NONE; } - internal static System.Net.IPAddress ToIPAddress (this string hostNameOrAddress) + internal static System.Net.IPAddress ToIPAddress ( + this string hostNameOrAddress) { try { var addrs = System.Net.Dns.GetHostAddresses (hostNameOrAddress); @@ -781,71 +783,70 @@ namespace WebSocketSharp } /// - /// Tries to create a for WebSocket with the specified . + /// Tries to create a for WebSocket with the specified + /// . /// /// - /// true if the is successfully created; otherwise, false. + /// true if a is successfully created; otherwise, + /// false. /// /// - /// A that contains a WebSocket URL to try. + /// A that represents the WebSocket URL to try. /// /// - /// When this method returns, a that represents the WebSocket URL - /// if is valid; otherwise, . + /// When this method returns, a that represents the + /// WebSocket URL if is valid; otherwise, + /// . /// /// - /// When this method returns, a that contains an error message - /// if is invalid; otherwise, String.Empty. + /// When this method returns, a that represents the + /// error message if is invalid; otherwise, + /// . /// - internal static bool TryCreateWebSocketUri (this string uriString, out Uri result, out string message) + internal static bool TryCreateWebSocketUri ( + this string uriString, out Uri result, out string message) { result = null; - if (uriString.Length == 0) - { + if (uriString.Length == 0) { message = "Must not be empty."; return false; } var uri = uriString.ToUri (); - if (!uri.IsAbsoluteUri) - { + if (!uri.IsAbsoluteUri) { message = "Must be the absolute URI: " + uriString; return false; } var scheme = uri.Scheme; - if (scheme != "ws" && scheme != "wss") - { + if (scheme != "ws" && scheme != "wss") { message = "The scheme part must be 'ws' or 'wss': " + scheme; return false; } var fragment = uri.Fragment; - if (fragment.Length != 0) - { + if (fragment.Length > 0) { message = "Must not contain the fragment component: " + uriString; return false; } var port = uri.Port; - if (port > 0) - { - if (port > 65535) - { + if (port > 0) { + if (port > 65535) { message = "The port part must be between 1 and 65535: " + port; return false; } - if ((scheme == "ws" && port == 443) || (scheme == "wss" && port == 80)) - { - message = String.Format ("Invalid pair of scheme and port: {0}, {1}", scheme, port); + if ((scheme == "ws" && port == 443) || (scheme == "wss" && port == 80)) { + message = String.Format ( + "Invalid pair of scheme and port: {0}, {1}", scheme, port); return false; } } - else - { + else { port = scheme == "ws" ? 80 : 443; - var url = String.Format ("{0}://{1}:{2}{3}", scheme, uri.Host, port, uri.PathAndQuery); + var url = String.Format ( + "{0}://{1}:{2}{3}", scheme, uri.Host, port, uri.PathAndQuery); uri = url.ToUri (); } @@ -867,8 +868,7 @@ namespace WebSocketSharp internal static void WriteBytes (this Stream stream, byte [] value) { - using (var src = new MemoryStream (value)) - { + using (var src = new MemoryStream (value)) { src.CopyTo (stream); } } @@ -878,12 +878,12 @@ namespace WebSocketSharp #region Public Methods /// - /// Determines whether the specified contains any of characters - /// in the specified array of . + /// Determines whether the specified contains any of + /// characters in the specified array of . /// /// - /// true if contains any of ; - /// otherwise, false. + /// true if contains any of + /// ; otherwise, false. /// /// /// A to test. @@ -901,53 +901,55 @@ namespace WebSocketSharp } /// - /// Determines whether the specified contains the entry - /// with the specified . + /// Determines whether the specified + /// contains the entry with the specified . /// /// - /// true if contains the entry with ; - /// otherwise, false. + /// true if contains the entry with + /// ; otherwise, false. /// /// /// A to test. /// /// - /// A that contains the key of the entry to find. + /// A that represents the key of the entry to find. /// public static bool Contains (this NameValueCollection collection, string name) { - return collection == null + return collection == null || collection.Count == 0 ? false : collection [name] != null; } /// - /// Determines whether the specified contains the entry - /// with the specified both and . + /// Determines whether the specified + /// contains the entry with the specified both and + /// . /// /// - /// true if contains the entry with both - /// and ; otherwise, false. + /// true if contains the entry with both + /// and ; otherwise, false. /// /// /// A to test. /// /// - /// A that contains the key of the entry to find. + /// A that represents the key of the entry to find. /// /// - /// A that contains the value of the entry to find. + /// A that represents the value of the entry to find. /// - public static bool Contains (this NameValueCollection collection, string name, string value) + public static bool Contains ( + this NameValueCollection collection, string name, string value) { - if (collection == null) + if (collection == null || collection.Count == 0) return false; var values = collection [name]; if (values == null) return false; - foreach (string v in values.Split (',')) + foreach (var v in values.Split (',')) if (v.Trim ().Equals (value, StringComparison.OrdinalIgnoreCase)) return true; @@ -955,7 +957,8 @@ namespace WebSocketSharp } /// - /// Emits the specified delegate if not . + /// Emits the specified delegate if it isn't + /// . /// /// /// A to emit. @@ -974,16 +977,17 @@ namespace WebSocketSharp } /// - /// Emits the specified EventHandler<TEventArgs> delegate if not . + /// Emits the specified EventHandler<TEventArgs> delegate if it + /// isn't . /// /// - /// An EventHandler<TEventArgs> to emit. + /// An EventHandler<TEventArgs> to emit. /// /// /// An from which emits this . /// /// - /// A TEventArgs that contains the event data. + /// A TEventArgs that represents the event data. /// /// /// The type of the event data generated by the event. @@ -997,19 +1001,23 @@ namespace WebSocketSharp } /// - /// Gets the collection of cookies from the specified . + /// Gets the collection of the HTTP Cookies from the specified HTTP + /// . /// /// - /// A that receives a collection of the HTTP Cookies. + /// A that receives a collection of the HTTP + /// Cookies. /// /// - /// A that contains a collection of the HTTP Headers. + /// A that contains a collection of the HTTP + /// Headers. /// /// - /// true if gets from the response ; - /// from the request , false. + /// true if is a collection of the response + /// headers; otherwise, false. /// - public static CookieCollection GetCookies (this NameValueCollection headers, bool response) + public static CookieCollection GetCookies ( + this NameValueCollection headers, bool response) { var name = response ? "Set-Cookie" : "Cookie"; return headers == null || !headers.Contains (name) @@ -1018,13 +1026,15 @@ namespace WebSocketSharp } /// - /// Gets the description of the HTTP status code using the specified . + /// Gets the description of the specified HTTP status . /// /// - /// A that contains the description of an HTTP status code. + /// A that represents the description of the HTTP status + /// code. /// /// - /// One of values that indicates an HTTP status code. + /// One of values that indicate the HTTP status + /// codes. /// public static string GetDescription (this HttpStatusCode code) { @@ -1032,40 +1042,45 @@ namespace WebSocketSharp } /// - /// Gets the name from the specified that contains a pair of name and value - /// separated by a separator string. + /// Gets the name from the specified that contains a pair + /// of name and value separated by a separator string. /// /// - /// A that contains the name if any; otherwise, null. + /// A that represents the name if any; otherwise, + /// null. /// /// - /// A that contains a pair of name and value separated by a separator string. + /// A that contains a pair of name and value separated by + /// a separator string. /// /// - /// A that contains a separator string. + /// A that represents a separator string. /// public static string GetName (this string nameAndValue, string separator) { - return (nameAndValue != null && nameAndValue.Length != 0) && - (separator != null && separator.Length != 0) + return (nameAndValue != null && nameAndValue.Length > 0) && + (separator != null && separator.Length > 0) ? nameAndValue.GetNameInternal (separator) : null; } /// - /// Gets the name and value from the specified that contains a pair of name and value - /// separated by a separator string. + /// Gets the name and value from the specified that + /// contains a pair of name and value separated by a separator string. /// /// - /// A KeyValuePair<string, string> that contains the name and value if any. + /// A KeyValuePair<string, string> that represents the name and + /// value if any. /// /// - /// A that contains a pair of name and value separated by a separator string. + /// A that contains a pair of name and value separated by + /// a separator string. /// /// - /// A that contains a separator string. + /// A that represents a separator string. /// - public static KeyValuePair GetNameAndValue (this string nameAndValue, string separator) + public static KeyValuePair GetNameAndValue ( + this string nameAndValue, string separator) { var name = nameAndValue.GetName (separator); var value = nameAndValue.GetValue (separator); @@ -1075,18 +1090,18 @@ namespace WebSocketSharp } /// - /// Gets the description of the HTTP status code using the specified . + /// Gets the description of the specified HTTP status . /// /// - /// A that contains the description of an HTTP status code. + /// A that represents the description of the HTTP status + /// code. /// /// - /// An that contains an HTTP status code. + /// An that represents the HTTP status code. /// public static string GetStatusDescription (this int code) { - switch (code) - { + switch (code) { case 100: return "Continue"; case 101: return "Switching Protocols"; case 102: return "Processing"; @@ -1139,22 +1154,24 @@ namespace WebSocketSharp } /// - /// Gets the value from the specified that contains a pair of name and value - /// separated by a separator string. + /// Gets the value from the specified that contains + /// a pair of name and value separated by a separator string. /// /// - /// A that contains the value if any; otherwise, null. + /// A that represents the value if any; otherwise, + /// null. /// /// - /// A that contains a pair of name and value separated by a separator string. + /// A that contains a pair of name and value separated by + /// a separator string. /// /// - /// A that contains a separator string. + /// A that represents a separator string. /// public static string GetValue (this string nameAndValue, string separator) { - return (nameAndValue != null && nameAndValue.Length != 0) && - (separator != null && separator.Length != 0) + return (nameAndValue != null && nameAndValue.Length > 0) && + (separator != null && separator.Length > 0) ? nameAndValue.GetValueInternal (separator) : null; } @@ -1192,7 +1209,8 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is enclosed in the specified . + /// Determines whether the specified is enclosed in the + /// specified . /// /// /// true if is enclosed in ; @@ -1202,7 +1220,7 @@ namespace WebSocketSharp /// A to test. /// /// - /// A that contains character to find. + /// A that represents the character to find. /// public static bool IsEnclosedIn (this string value, char c) { @@ -1213,10 +1231,12 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is host (this computer architecture) byte order. + /// Determines whether the specified is host (this + /// computer architecture) byte order. /// /// - /// true if is host byte order; otherwise, false. + /// true if is host byte order; otherwise, + /// false. /// /// /// A to test. @@ -1229,8 +1249,8 @@ namespace WebSocketSharp } /// - /// Determines whether the specified represents - /// the local IP address. + /// Determines whether the specified + /// represents the local IP address. /// /// /// true if represents the local IP address; @@ -1261,10 +1281,12 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is or empty. + /// Determines whether the specified is + /// or empty. /// /// - /// true if is or empty; otherwise, false. + /// true if is or + /// empty; otherwise, false. /// /// /// A to test. @@ -1275,53 +1297,54 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is a predefined scheme. + /// Determines whether the specified is a predefined + /// scheme. /// /// - /// true if is a predefined scheme; otherwise, false. + /// true if is a predefined scheme; otherwise, + /// false. /// - /// + /// /// A to test. /// - public static bool IsPredefinedScheme (this string scheme) + public static bool IsPredefinedScheme (this string value) { - if (scheme == null && scheme.Length < 2) + if (value == null && value.Length < 2) return false; - char c = scheme [0]; + var c = value [0]; if (c == 'h') - return scheme == "http" || scheme == "https"; - - if (c == 'f') - return scheme == "file" || scheme == "ftp"; + return value == "http" || value == "https"; if (c == 'w') - return scheme == "ws" || scheme == "wss"; + return value == "ws" || value == "wss"; + + if (c == 'f') + return value == "file" || value == "ftp"; - if (c == 'n') - { - c = scheme [1]; + if (c == 'n') { + c = value [1]; return c == 'e' - ? scheme == "news" || scheme == "net.pipe" || scheme == "net.tcp" - : scheme == "nntp"; + ? value == "news" || value == "net.pipe" || value == "net.tcp" + : value == "nntp"; } - return (c == 'g' && scheme == "gopher") || (c == 'm' && scheme == "mailto"); + return (c == 'g' && value == "gopher") || (c == 'm' && value == "mailto"); } /// - /// Determines whether the specified is an HTTP Upgrade request - /// to switch to the specified . + /// Determines whether the specified is an + /// HTTP Upgrade request to switch to the specified . /// /// - /// true if is an HTTP Upgrade request - /// to switch to ; otherwise, false. + /// true if is an HTTP Upgrade request to + /// switch to ; otherwise, false. /// /// - /// A that contains an HTTP request information. + /// A that represents the HTTP request. /// /// - /// A that contains a protocol name. + /// A that represents the protocol name. /// /// /// @@ -1335,9 +1358,10 @@ namespace WebSocketSharp /// /// /// - /// is . + /// is empty. /// - public static bool IsUpgradeTo (this HttpListenerRequest request, string protocol) + public static bool IsUpgradeTo ( + this HttpListenerRequest request, string protocol) { if (request == null) throw new ArgumentNullException ("request"); @@ -1356,24 +1380,25 @@ namespace WebSocketSharp /// Determines whether the specified is a URI string. /// /// - /// true if is maybe a URI string; otherwise, false. + /// true if is maybe a URI string; otherwise, + /// false. /// - /// + /// /// A to test. /// - public static bool MaybeUri (this string uriString) + public static bool MaybeUri (this string value) { - if (uriString == null || uriString.Length == 0) + if (value == null || value.Length == 0) return false; - int p = uriString.IndexOf (':'); - if (p == -1) + var i = value.IndexOf (':'); + if (i == -1) return false; - if (p >= 10) + if (i >= 10) return false; - return uriString.Substring (0, p).IsPredefinedScheme (); + return value.Substring (0, i).IsPredefinedScheme (); } /// @@ -1381,16 +1406,19 @@ namespace WebSocketSharp /// A sub-array starts at the specified element position. /// /// - /// An array of T that receives a sub-array, or an empty array of T if any problems with the parameters. + /// An array of T that receives a sub-array, or an empty array of T if any + /// problems with the parameters. /// /// /// An array of T that contains the data to retrieve a sub-array. /// /// - /// An that contains the zero-based starting position of a sub-array in . + /// An that contains the zero-based starting position of + /// a sub-array in . /// /// - /// An that contains the number of elements to retrieve a sub-array. + /// An that contains the number of elements to retrieve + /// a sub-array. /// /// /// The type of elements in the . @@ -1398,13 +1426,13 @@ namespace WebSocketSharp public static T [] SubArray (this T [] array, int startIndex, int length) { if (array == null || array.Length == 0) - return new T []{}; + return new T [0]; if (startIndex < 0 || length <= 0) - return new T []{}; + return new T [0]; if (startIndex + length > array.Length) - return new T []{}; + return new T [0]; if (startIndex == 0 && array.Length == length) return array; @@ -1416,7 +1444,8 @@ namespace WebSocketSharp } /// - /// Executes the specified delegate times. + /// Executes the specified delegate + /// times. /// /// /// An is the number of times to execute. @@ -1431,7 +1460,8 @@ namespace WebSocketSharp } /// - /// Executes the specified delegate times. + /// Executes the specified delegate + /// times. /// /// /// A is the number of times to execute. @@ -1439,14 +1469,15 @@ namespace WebSocketSharp /// /// An delegate that references the method(s) to execute. /// - public static void Times( this long n, Action act) + public static void Times (this long n, Action act) { if (n > 0 && act != null) ((ulong) n).times (act); } /// - /// Executes the specified delegate times. + /// Executes the specified delegate + /// times. /// /// /// A is the number of times to execute. @@ -1461,7 +1492,8 @@ namespace WebSocketSharp } /// - /// Executes the specified delegate times. + /// Executes the specified delegate + /// times. /// /// /// A is the number of times to execute. @@ -1476,14 +1508,16 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<int> delegate times. + /// Executes the specified Action<int> delegate + /// times. /// /// /// An is the number of times to execute. /// /// - /// An Action<int> delegate that references the method(s) to execute. - /// An parameter to pass to the method(s) is the zero-based count of iteration. + /// An Action<int> delegate that references the method(s) to + /// execute. An parameter to pass to the method(s) is the + /// zero-based count of iteration. /// public static void Times (this int n, Action act) { @@ -1493,14 +1527,16 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<long> delegate times. + /// Executes the specified Action<long> delegate + /// times. /// /// /// A is the number of times to execute. /// /// - /// An Action<long> delegate that references the method(s) to execute. - /// A parameter to pass to the method(s) is the zero-based count of iteration. + /// An Action<long> delegate that references the method(s) to + /// execute. A parameter to pass to the method(s) is the + /// zero-based count of iteration. /// public static void Times (this long n, Action act) { @@ -1510,14 +1546,16 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<uint> delegate times. + /// Executes the specified Action<uint> delegate + /// times. /// /// /// A is the number of times to execute. /// /// - /// An Action<uint> delegate that references the method(s) to execute. - /// A parameter to pass to the method(s) is the zero-based count of iteration. + /// An Action<uint> delegate that references the method(s) to + /// execute. A parameter to pass to the method(s) is the + /// zero-based count of iteration. /// public static void Times (this uint n, Action act) { @@ -1527,14 +1565,16 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<ulong> delegate times. + /// Executes the specified Action<ulong> delegate + /// times. /// /// /// A is the number of times to execute. /// /// - /// An Action<ulong> delegate that references the method(s) to execute. - /// A parameter to pass to this method(s) is the zero-based count of iteration. + /// An Action<ulong> delegate that references the method(s) to + /// execute. A parameter to pass to this method(s) is the + /// zero-based count of iteration. /// public static void Times (this ulong n, Action act) { @@ -1544,12 +1584,13 @@ namespace WebSocketSharp } /// - /// Converts the specified array of to the specified type data. + /// Converts the specified array of to the specified type + /// data. /// /// - /// A T converted from , or a default value of T - /// if is an empty array of - /// or if the type of T isn't , , , + /// A T converted from , or a default value of T if + /// is an empty array of or if the + /// type of T isn't , , , /// , , , , /// , or . /// @@ -1557,7 +1598,8 @@ namespace WebSocketSharp /// An array of to convert. /// /// - /// A that indicates the byte order of . + /// A that indicates the byte order of + /// . /// /// /// The type of the return. The T must be a value type. @@ -1601,7 +1643,8 @@ namespace WebSocketSharp } /// - /// Converts the specified to an array of . + /// Converts the specified to an array of + /// . /// /// /// An array of converted from . @@ -1649,7 +1692,8 @@ namespace WebSocketSharp } /// - /// Converts the order of the specified array of to the host byte order. + /// Converts the order of the specified array of to the + /// host byte order. /// /// /// An array of converted from . @@ -1658,7 +1702,8 @@ namespace WebSocketSharp /// An array of to convert. /// /// - /// A that indicates the byte order of . + /// One of values that indicate the byte order of + /// . /// /// /// is . @@ -1674,18 +1719,19 @@ namespace WebSocketSharp } /// - /// Converts the specified to a that concatenates - /// the each element of across the specified . + /// Converts the specified to a + /// that concatenates the each element of across the + /// specified . /// /// - /// A converted from , or a - /// if is empty. + /// A converted from , or + /// if it's empty. /// /// /// An array of T to convert. /// /// - /// A that contains a separator string. + /// A that represents the separator string. /// /// /// The type of elements in . @@ -1706,9 +1752,8 @@ namespace WebSocketSharp separator = String.Empty; var buffer = new StringBuilder (64); - (len - 1).Times (i => - buffer.AppendFormat ("{0}{1}", array [i].ToString (), separator) - ); + (len - 1).Times ( + i => buffer.AppendFormat ("{0}{1}", array [i].ToString (), separator)); buffer.Append (array [len - 1].ToString ()); return buffer.ToString (); @@ -1719,8 +1764,7 @@ namespace WebSocketSharp /// /// /// A converted from , or - /// if is - /// or empty. + /// if it's or empty. /// /// /// A to convert. @@ -1738,49 +1782,52 @@ namespace WebSocketSharp /// URL-decodes the specified . /// /// - /// A that receives a decoded string, or the - /// if is or . + /// A that receives the decoded string, or the + /// if it's or empty. /// - /// + /// /// A to decode. /// - public static string UrlDecode (this string s) + public static string UrlDecode (this string value) { - return s == null || s.Length == 0 - ? s - : HttpUtility.UrlDecode (s); + return value == null || value.Length == 0 + ? value + : HttpUtility.UrlDecode (value); } /// /// URL-encodes the specified . /// /// - /// A that receives a encoded string, or the - /// if is or . + /// A that receives the encoded string, or + /// if it's or empty. /// - /// + /// /// A to encode. /// - public static string UrlEncode (this string s) + public static string UrlEncode (this string value) { - return s == null || s.Length == 0 - ? s - : HttpUtility.UrlEncode (s); + return value == null || value.Length == 0 + ? value + : HttpUtility.UrlEncode (value); } /// - /// Writes the specified content data using the specified . + /// Writes the specified data using the specified + /// . /// /// - /// A that contains a network stream to write a content data. + /// A that represents the HTTP response + /// used to write the content data. /// /// - /// An array of that contains a content data to write. + /// An array of that contains the content data to write. /// /// /// is . /// - public static void WriteContent (this HttpListenerResponse response, byte [] content) + public static void WriteContent ( + this HttpListenerResponse response, byte [] content) { if (response == null) throw new ArgumentNullException ("response");