Index: Src/GoogleApis/Apis/Utilities.cs =================================================================== deleted file mode 100644 --- a/Src/GoogleApis/Apis/Utilities.cs +++ /dev/null @@ -1,241 +0,0 @@ -/* -Copyright 2010 Google Inc - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -/* Change history -* Oct 13 2008 Joe Feser joseph.feser@gmail.com -* Converted Lists and other .NET 1.1 collections to use Generics -* Combined IExtensionElement and IExtensionElementFactory interfaces -* -*/ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Xml; -using System.Reflection; - -using Google.Apis.Util; - -namespace Google.Apis -{ - /// String utilities. - public static class Utilities - { - /// True when the Mono-runtime is used to execute this code. - public static readonly bool IsRunningOnMono = (Type.GetType("Mono.Runtime") != null); - - /// xsd version of bool:true. - public const string XSDTrue = "true"; - - /// xsd version of bool:false. - public const string XSDFalse = "false"; - - /// returns a blank emptyDate. That's the default for an empty string date - public static DateTime EmptyDate - { - get - { - // that's the blank value you get when setting a DateTime to an empty string inthe property browswer - return new DateTime(1, 1, 1); - } - } - - /// Helper to read in a string and Encode it. - /// the xmlreader string - /// UTF8 encoded string - public static string EncodeString(string content) - { - // get the encoding - Encoding utf8Encoder = Encoding.UTF8; - - Byte[] utf8Bytes = EncodeStringToUtf8(content); - - char[] utf8Chars = new char[utf8Encoder.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)]; - utf8Encoder.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0); - - String utf8String = new String(utf8Chars); - - return utf8String; - } - - /// - /// returns you a bytearray of UTF8 bytes from the string passed in - /// the passed in string is assumed to be UTF16 - /// - /// UTF16 string - /// utf 8 byte array - public static Byte[] EncodeStringToUtf8(string content) - { - // get the encoding - Encoding utf8Encoder = Encoding.UTF8; - Encoding utf16Encoder = Encoding.Unicode; - - Byte[] bytes = utf16Encoder.GetBytes(content); - - Byte[] utf8Bytes = Encoding.Convert(utf16Encoder, utf8Encoder, bytes); - return utf8Bytes; - } - - /// helper to read in a string and Encode it according to - /// RFC 5023 rules for slugheaders - /// the Unicode string for the slug header - /// ASCII encoded string - public static string EncodeSlugHeader(string slug) - { - if (slug == null) - { - return ""; - } - - Byte[] bytes = EncodeStringToUtf8(slug); - - if (bytes == null) - { - return ""; - } - - StringBuilder returnString = new StringBuilder(256); - - foreach (byte b in bytes) - { - if ((b < 0x20) || (b == 0x25) || (b > 0x7E)) - { - returnString.AppendFormat(CultureInfo.InvariantCulture, "%{0:X}", b); - } - else - { - returnString.Append((char)b); - } - } - - return returnString.ToString(); - } - - /// - /// tests an etag for weakness. returns TRUE for weak etags and for null strings - /// - /// - /// - public static bool IsWeakETag(string eTag) - { - if (eTag == null) - { - return true; - } - if (eTag.StartsWith("W/")) - { - return true; - } - return false; - } - - /// Method to output just the date portion as string - /// the DateTime object to output as a string - /// an rfc-3339 string - public static string LocalDateInUTC(DateTime dateTime) - { - // Add "full-date T partial-time" - return dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); - } - - /// Helper method to format a TimeSpan as a string compliant with the "time-offset" format defined in RFC-3339 - /// the TimeSpan to format - /// - public static string FormatTimeOffset(TimeSpan spanFromUtc) - { - // Simply return "Z" if there is no offset - if (spanFromUtc == TimeSpan.Zero) - { - return "Z"; - } - - // Return the numeric offset - TimeSpan absoluteSpan = spanFromUtc.Duration(); - if (spanFromUtc > TimeSpan.Zero) - { - return "+" + FormatNumOffset(absoluteSpan); - } - else - { - return "-" + FormatNumOffset(absoluteSpan); - } - } - - /// Helper method to format a TimeSpan to {HH}:{MM} - /// the TimeSpan to format - /// a string in "hh:mm" format. - internal static string FormatNumOffset(TimeSpan timeSpan) - { - return String.Format(CultureInfo.InvariantCulture, "{0:00}:{1:00}", timeSpan.Hours, timeSpan.Minutes); - } - - ///////////////////////////////////////////////////////////////////////////// - - /// Returns the version of the Core library. - public static string GetLibraryVersion() - { - return Regex.Match(typeof(Utilities).Assembly.FullName, "Version=([\\d\\.]+)").Groups[1].ToString(); - } - - /// - /// Replaces all the specified characters within the input string with the given replacement - /// - public static string Replace(this string input, string replace, params char[] invalidCharacters) - { - invalidCharacters.ThrowIfNullOrEmpty("invalidCharacters"); - - // Create the resulting string - var result = new StringBuilder(input.Length); - - foreach (char c in input) - { - // Replace invalid characters with the replacement string - if (invalidCharacters.Contains(c)) - { - result.Append(replace); - continue; - } - - result.Append(c); - } - - return result.ToString(); - } - - #region LINQ extensions - - /// - /// Returns the enumerable with the specified element removed - /// - public static IEnumerable Without(this IEnumerable enumerable, T toRemove) - { - return enumerable.Except(new[] { toRemove }); - } - - /// - /// Returns the enumerable with the specified element added to the end of it. - /// - public static IEnumerable Concat(this IEnumerable enumerable, T toAdd) - { - return enumerable.Concat(new[] { toAdd }); - } - - #endregion - } -} \ No newline at end of file