1 /**
2  * An minimal implementation of the deprecated octal literals
3  *
4  * Copyright: Copyright (C) 2011 Pavel Sountsov.  All rights reserved.
5  * License:   BSD style: $(LICENSE)
6  * Authors:   Pavel Sountsov
7  */ 
8 module tango.core.Octal;
9 
10 T toOctal(T)(T decimal)
11 {
12     T ret = 0;
13     uint power = 0;
14     int sign = 1;
15     if(decimal < 0)
16     {
17         decimal = -decimal;
18         sign = -1;
19     }
20     while(decimal > 0)
21     {
22         int digit = decimal % 10;
23         assert(digit < 8, "Only digits [0..7] are allowed in octal literals");
24         ret += digit << power;
25         
26         decimal /= 10;
27         power += 3;
28     }
29     
30     return ret * sign;
31 }
32 
33 template octal(int decimal)
34 {
35     enum octal = toOctal(decimal);
36 }
37 
38 template octalU(uint decimal)
39 {
40     enum octal = toOctal(decimal);
41 }
42 
43 template octalL(long decimal)
44 {
45     enum octal = toOctal(decimal);
46 }
47 
48 template octalUL(ulong decimal)
49 {
50     enum octal = toOctal(decimal);
51 }
52 
53 debug(UnitTest)
54 {
55     unittest
56     {
57         assert(octal!(764) == 500);
58         assert(octal!(1) == 1);
59         assert(octal!(0) == 0);
60         assert(octal!(-10) == -8);
61     }
62 }