1 /******************************************************************************* 2 3 Utilities for common CTFE operations needed for code generation 4 5 NB: because this module is often used as purely compile-time dependency it 6 used built-in asserts instead of `ocean.core.Test` to reduce amount of 7 cyclic imports. `ocean.meta` modules in general are not supposed to 8 import anything outside of `ocean.meta`. 9 10 Copyright: 11 Copyright (c) 2017 dunnhumby Germany GmbH. All rights reserved. 12 13 License: 14 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 15 Alternatively, this file may be distributed under the terms of the Tango 16 3-Clause BSD License (see LICENSE_BSD.txt for details). 17 18 *******************************************************************************/ 19 20 module ocean.meta.codegen.CTFE; 21 22 import ocean.meta.types.Qualifiers; 23 24 /******************************************************************************* 25 26 Converts integer to string 27 28 Params: 29 i = integer to convert 30 31 *******************************************************************************/ 32 33 istring toString ( T ) ( T i ) 34 { 35 if (i == 0) 36 return "0"; 37 38 if (i < 0) 39 return "-" ~ toStringImpl(- cast(long) i); 40 41 return toStringImpl(cast(ulong) i); 42 } 43 44 unittest 45 { 46 static assert (toString(0L) == "0"); 47 static assert (toString(42L) == "42"); 48 static assert (toString(-42L) == "-42"); 49 static assert (toString(31L) == "31"); 50 static assert (toString(-31L) == "-31"); 51 static assert (toString(14L) == "14"); 52 static assert (toString(long.max) == "9223372036854775807"); 53 static assert (toString(long.min) == "-9223372036854775808"); 54 static assert (toString(int.min) == "-2147483648"); 55 static assert (toString(int.max) == "2147483647"); 56 static assert (toString(short.min) == "-32768"); 57 static assert (toString(short.max) == "32767"); 58 } 59 60 unittest 61 { 62 assert (toString(14UL) == "14"); 63 static assert (toString(14UL) == "14"); 64 assert (toString(ulong.max) == "18446744073709551615"); 65 static assert (toString(ulong.max) == "18446744073709551615"); 66 } 67 68 unittest 69 { 70 assert (toString(cast(short) 14) == "14"); 71 } 72 73 istring toStringImpl ( ulong i ) 74 { 75 istring digit = "0123456789"; 76 77 if (i == 0) 78 return "0"; 79 80 istring res; 81 82 while (i > 0) 83 { 84 res = digit[i % 10] ~ res; 85 i /= 10; 86 } 87 88 return res; 89 } 90 91 /******************************************************************************* 92 93 Utility to strip the part before the '.' in a string. 94 95 Params: 96 name = string to scan 97 98 *******************************************************************************/ 99 100 public istring stripQualifiedPrefix ( istring name ) 101 { 102 ptrdiff_t idx = name.length - 1; 103 104 while (idx > 0 && name[idx] != '.') 105 --idx; 106 107 if (idx == 0) 108 return name; 109 else 110 return name[idx + 1 .. $]; 111 } 112 113 unittest 114 { 115 assert (stripQualifiedPrefix("aa.bb") == "bb"); 116 static assert (stripQualifiedPrefix("aa.bb") == "bb"); 117 assert (stripQualifiedPrefix("aa.bb.cc") == "cc"); 118 static assert (stripQualifiedPrefix("aa.bb.cc") == "cc"); 119 assert (stripQualifiedPrefix("aabb") == "aabb"); 120 static assert (stripQualifiedPrefix("aabb") == "aabb"); 121 }