1 module PixelPerfectEngine.system.etc;
2 
3 import std.conv;
4 import std.algorithm.mutation;
5 /*
6  * Copyright (C) 2015-2017, by Laszlo Szeremi under the Boost license.
7  *
8  * Pixel Perfect Engine, etc module
9  */
10 
11 
12 ///Converts string[] to wstring[]
13 public wstring[] stringArrayConv(string[] s){
14 	wstring[] result;
15 	foreach(ss; s){
16 		wstring ws;
17 		foreach(c; ss){
18 			ws ~= c;
19 		}
20 		result ~= ws;
21 	}
22 	return result;
23 }
24 ///Returns a hexadecimal string representation of the integer.
25 string intToHex(int i, int format = 0){
26 	string result;
27 	do{
28 		switch(i & 0x000F){
29 			case 1: result ~='1'; break;
30 			case 2: result ~='2'; break;
31 			case 3: result ~='3'; break;
32 			case 4: result ~='4'; break;
33 			case 5: result ~='5'; break;
34 			case 6: result ~='6'; break;
35 			case 7: result ~='7'; break;
36 			case 8: result ~='8'; break;
37 			case 9: result ~='9'; break;
38 			case 10: result ~='A'; break;
39 			case 11: result ~='B'; break;
40 			case 12: result ~='C'; break;
41 			case 13: result ~='D'; break;
42 			case 14: result ~='E'; break;
43 			case 15: result ~='F'; break;
44 			default: result ~='0'; break;
45 		}
46 		i = i >>> 4;
47 	}while(i > 0);
48 	if(result.length < format){
49 		for(int j = result.length ; j < format ; j++){
50 			result ~= '0';
51 		}
52 	}
53 	reverse(cast(char[])result);
54 	return result;
55 }
56 ///Returns a octal string representation of the integer.
57 string intToOct(int i, int format){
58 	string result;
59 	do{
60 		switch(i & 0x0007){
61 			case 1: result ~='1'; break;
62 			case 2: result ~='2'; break;
63 			case 3: result ~='3'; break;
64 			case 4: result ~='4'; break;
65 			case 5: result ~='5'; break;
66 			case 6: result ~='6'; break;
67 			case 7: result ~='7'; break;
68 			default: result ~='0'; break;
69 		}
70 		i = i / 8;
71 	}while(i > 0);
72 	if(result.length < format){
73 		for(int j = result.length ; j < format ; j++){
74 			result ~= '0';
75 		}
76 	}
77 	reverse(cast(char[])result);
78 	return result;
79 }
80 ///Parses a hexadecimal int represented as a string.
81 int parseHex(string s){
82 	//std.stdio.writeln(s);
83 	int result;
84 	for(int i ; i < s.length; i++){
85 		result *= 16;
86 		switch(s[i]){
87 			case '0': break;
88 			case '1': result += 1; break;
89 			case '2': result += 2; break;
90 			case '3': result += 3; break;
91 			case '4': result += 4; break;
92 			case '5': result += 5; break;
93 			case '6': result += 6; break;
94 			case '7': result += 7; break;
95 			case '8': result += 8; break;
96 			case '9': result += 9; break;
97 			case 'a','A': result += 10; break;
98 			case 'b','B': result += 11; break;
99 			case 'c','C': result += 12; break;
100 			case 'd','D': result += 13; break;
101 			case 'e','E': result += 14; break;
102 			case 'f','F': result += 15; break;
103 			default: throw new Exception("String cannot be parsed!"); 
104 		}
105 	}
106 	return result;
107 }
108 ///Parses a comma separated string into a single array.
109 string[] csvParser(string input, char separator = ','){
110 	string[] result;
111 	int j;
112 	for(int i ; i < input.length ; i++){
113 		if(input[i] == separator){
114 			result ~= input[j..i];
115 			j = i + 1;
116 		}
117 	}
118 
119 	return result;
120 }
121 ///Joins prettyprint strings to a single string for file storage.
122 string stringArrayJoin(string[] input){
123 	string result;
124 	foreach(string s ; input){
125 		result ~= s ~ "\n";
126 	}
127 	return result;
128 }
129 ///
130 bool isInteger(S)(S s){
131 	static if(S.mangleof == string.mangleof || S.mangleof == wstring.mangleof || S.mangleof == dstring.mangleof){
132 		foreach(c; s){
133 			if(c > '9' || c < '0')
134 				return false;
135 		}
136 		return true;
137 	}else static assert("Template patameter " ~ S.stringof ~ " not supported in function 'bool isInteger(S)(S s)' of module 'PixelPerfectEngine.system.etc'");
138 }