Pretty-print.
[beagle.git] / Util / GuidFu.cs
blob65b572fceee6bf5669aa28be810de618ea90610a
1 //
2 // GuidFu.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
29 namespace Beagle.Util {
31 public class GuidFu {
33 public const string UriScheme = "uid";
35 static private string UriPrefix = UriScheme + ":";
37 static public string ToShortString (Guid uid)
39 // When converting 16 bytes to base64, the final two characters
40 // are always "==".
41 string str = Convert.ToBase64String (uid.ToByteArray (), 0, 16);
42 str = str.Substring (0, 22);
43 str = str.Replace ('/', '_'); // this avoids problems w/ Uris
44 return str;
47 static public Guid FromShortString (string str)
49 // We need to add back the padding before converting the
50 // string back to a byte array.
51 str = str.Replace ('_', '/') + "==";
52 return new Guid (Convert.FromBase64String (str));
55 static public Uri ToUri (Guid uid)
57 return new Uri (ToUriString (uid), true);
60 static public Uri FromShortStringToUri (string str)
62 return new Uri (UriPrefix + str , true);
65 static public Guid FromUri (Uri uri)
67 return FromShortString (uri.LocalPath);
70 static public string ToUriString (Guid uid)
72 return UriPrefix + ToShortString (uid);
75 static public Guid FromUriString (string str)
77 return FromShortString (str.Substring (UriPrefix.Length));