root/trunk/src/semitwist/util/ver.d

Revision 240, 1.9 kB (checked in by Abscissa, 6 months ago)

Fixed for 64bit

  • Property svn:eol-style set to native
Line 
1 // SemiTwist D Tools: Library
2 // Written in the D programming language.
3
4 module semitwist.util.ver;
5
6 import std.stdio;
7 import std.math;
8 import std.conv;
9 import std.string;
10
11 import semitwist.util.all;
12
13 //TODO: Support versions that have different semantics
14 //TODO: Document ordering semantics of this
15 //TODO: This should all work at compile-time
16 struct Ver
17 {
18     uint[] ver;
19    
20     const int opCmp(ref const(Ver) v)
21     {
22         for(int i=0; i < reduce!"a<b?a:b"([this.ver.length, v.ver.length]); i++)
23         {
24             if(this.ver[i] != v.ver[i])
25                 return this.ver[i] - v.ver[i];
26         }
27        
28         if(this.ver.length > v.ver.length)
29             return 1;
30         else if(this.ver.length < v.ver.length)
31             return -1;
32        
33         return 0;
34     }
35    
36     const bool opEquals(ref const(Ver) v)
37     {
38         return this.opCmp(v) == 0;
39     }
40    
41     const hash_t toHash()
42     {
43         auto str = toString();
44         return typeid(string).getHash(&str);
45     }
46
47     const string toString()
48     {
49         return join(to!(string[])(ver), ".");
50     }
51 }
52
53 Ver toVer(string str)
54 {
55     auto strParts = str.ctfe_split(".");
56     uint[] verParts;
57     verParts.length = strParts.length;
58    
59     foreach(i, strPart; strParts)
60         verParts[i] = ctfe_to!uint(strPart);
61        
62     return Ver(verParts);
63 }
64
65 mixin(unittestSemiTwistDLib(q{
66     mixin(deferAssert!(`Ver([5,5,5])  == Ver([5,5,5])`));
67     mixin(deferAssert!(`Ver([5,5,0])  != Ver([5,5,5])`));
68     mixin(deferAssert!(`Ver([5,5])    != Ver([5,5,5])`));
69     mixin(deferAssert!(`Ver([2,10,3]) == Ver([2,10,3])`));
70
71     mixin(deferAssert!(`Ver([5,5,5]) > Ver([5,5,1])`));
72     mixin(deferAssert!(`Ver([5,5,5]) > Ver([5,1,5])`));
73     mixin(deferAssert!(`Ver([5,5,5]) > Ver([1,5,5])`));
74
75     mixin(deferAssert!(`Ver([5,5,0]) < Ver([5,5,5])`));
76
77     mixin(deferAssert!(`Ver([5,5,0]) > Ver([5,5])`));
78     mixin(deferAssert!(`Ver([5,5])   < Ver([5,5,0])`));
79
80     mixin(deferAssert!(`Ver([1,10]) > Ver([1,1])`));
81
82     mixin(deferEnsure!(`"2.10.3".toVer().ver`, `_ == [cast(uint)2,10,3]`));
83     mixin(deferEnsure!(`"2.10.3".toVer()`, `_ == Ver([2,10,3])`));
84     mixin(deferEnsure!(`Ver([2,10,3]).toString()`, `_ == "2.10.3"`));
85 }));
Note: See TracBrowser for help on using the browser.