OSDN Git Service

initial build for opensource
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages / VersionComparetor.cs
1 using System;\r
2 using System.Text.RegularExpressions;\r
3 \r
4 namespace NaGet.Packages\r
5 {\r
6         // TODO Debian-apt\82Ì\83R\81[\83h\82ð\97\98\97p\82µ\82Ä\82¢\82é\82Ì\82ÅGPL\82É\82È\82é\82Ì\82É\92\8d\88Ó\r
7         \r
8         \r
9         public class VersionComparetor : System.Collections.Generic.IComparer<string>\r
10         {\r
11                 public VersionComparetor()\r
12                 {\r
13                 }\r
14                 \r
15                 private static int order(char x)\r
16                 {\r
17                         return (char.IsDigit(x) ? 0\r
18                                 : (x==0) ? 0\r
19                                 : char.IsLetter(x) ? (x)\r
20                                 : (x) + 256);\r
21                 }\r
22                 \r
23                 public int Compare(string a, string b)\r
24                 {\r
25                         // \91O\8f\88\97\9d\r
26                         a = a.ToLower();\r
27                         b = b.ToLower();\r
28                         if (Regex.IsMatch(a, @"\.0*$")) \r
29                                 Regex.Replace(a, @"\.0*$", string.Empty);\r
30                         if (Regex.IsMatch(b, @"\.0*$")) \r
31                                 Regex.Replace(b, @"\.0*$", string.Empty);\r
32                         \r
33                         if (a == b) {\r
34                                 return 0;\r
35                         }\r
36                         \r
37                         \r
38                         int apos = 0, bpos = 0;\r
39                         int alen = a.Length, blen = b.Length;\r
40                         \r
41                         while ((apos < alen) && (bpos < blen)) {\r
42                                 int first_diff = 0;\r
43                                 \r
44                                 while ((apos < alen) && (bpos < blen) &&\r
45                                        (!char.IsDigit(a[apos]) || !char.IsDigit(b[bpos])) ) {\r
46                                         int vc = order(a[apos]);\r
47                                         int rc = order(b[bpos]);\r
48                                         if (vc != rc)\r
49                                                 return vc - rc;\r
50                                         apos ++; bpos ++;\r
51                                 }\r
52                                 \r
53                                 if (a[apos] == '0') apos ++;\r
54                                 if (b[bpos] == '0') bpos ++;\r
55                                 \r
56                                 while ((apos < alen) && (bpos < blen) &&\r
57                                        char.IsDigit(a[apos]) && char.IsDigit(b[bpos])) {\r
58                                         if (first_diff == 0)\r
59                                                 first_diff = a[apos] - b[bpos];\r
60                                         apos ++; bpos ++;\r
61                                 }\r
62                                 \r
63                                 if (apos < alen && char.IsDigit(a[apos])) {\r
64                                         return 1;\r
65                                 } else if (bpos < blen && char.IsDigit(b[bpos])) {\r
66                                         return -1;\r
67                                 } else if (first_diff != 0) {\r
68                                         return first_diff;\r
69                                 }\r
70                         }\r
71                         \r
72                         if (apos == alen && bpos == blen) {\r
73                                 return 0;\r
74                         } else if (apos == alen) {\r
75                                 return -1;\r
76                         } else if (bpos == blen) {\r
77                                 return 1;\r
78                         } else {\r
79                                 return 1; // Shouldnt happen\r
80                         }\r
81                 }\r
82                 \r
83                 \r
84         }\r
85 }\r