using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace Nuclex.Networking.Gallery3 { /// Contains helper methods for working with gallery URLs internal static class UrlHelper { /// Characters the represent numbers private static char[] Numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; /// Extracts the ids from a list of URLs ending in an ID /// Array of urls the ids will be extracted from /// An array of ids matching the IDs contained in the provided urls public static int[] ExtractIdsFromUrls(string[] urls) { int[] ids = new int[urls.Length]; for (int index = 0; index < urls.Length; ++index) { int finalSlashIndex = urls[index].LastIndexOf('/'); if (finalSlashIndex == -1) { Debug.Assert( false, "List contained an item that's not an URL", "The URL list contained an entry that was not an URL ending in " + "an ID and thus could not be parsed for its ID" ); finalSlashIndex = 0; // We try to be optimistic (may still fail right below) } else { ++finalSlashIndex; } int finalNumberIndex = urls[index].indexNotOfAny(Numbers, finalSlashIndex); if (finalNumberIndex == -1) { finalNumberIndex = urls[index].Length; } // Parse the ID from the URL and add it to our list ids[index] = int.Parse( urls[index].Substring(finalSlashIndex, finalNumberIndex - finalSlashIndex) ); } return ids; } /// /// Searches for the first occurence of a character other than the characters /// listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// private static int indexNotOfAny(this string haystack, char[] anyNotOf) { return indexNotOfAny(haystack, anyNotOf, 0, haystack.Length); } /// /// Searches for the first occurence of a character other than the characters /// listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// Index of the character in the haystack at which to start scanning /// /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// private static int indexNotOfAny(this string haystack, char[] anyNotOf, int startIndex) { return indexNotOfAny(haystack, anyNotOf, startIndex, haystack.Length - startIndex); } /// /// Searches for the first occurence of a character other than the characters /// listed in the parameter /// /// String that will be scanned in /// Characters to not look for in the scanned string /// /// Index of the character in the haystack at which to start scanning /// /// Number of characters in the haystack to scan /// /// The index of the first occurence of a character not in the /// array or -1 if all characters in the string were /// present in the array. /// private static int indexNotOfAny( this string haystack, char[] anyNotOf, int startIndex, int count ) { int anyLength = anyNotOf.Length; count += startIndex; while (startIndex < count) { char character = haystack[startIndex]; int index = Array.IndexOf(anyNotOf, character, 0, anyLength); if (index == -1) { return startIndex; } ++startIndex; } return -1; } } } // namespace Nuclex.Networking.Gallery3