PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/services/authcharts/lib/chx/text/Sprintf.class.php

https://github.com/precog/visualizations
PHP | 349 lines | 348 code | 1 blank | 0 comment | 19 complexity | 6acfc85dbc08615ac740fb49b66985b1 MD5 | raw file
  1. <?php
  2. class chx_text_Sprintf {
  3. public function __construct(){}
  4. static $kPAD_ZEROES = 1;
  5. static $kLEFT_ALIGN = 2;
  6. static $kSHOW_SIGN = 4;
  7. static $kPAD_POS = 8;
  8. static $kALT_FORM = 16;
  9. static $kLONG_VALUE = 32;
  10. static $kUSE_SEPARATOR = 64;
  11. static $DEBUG = false;
  12. static $TRACE = false;
  13. static function format($format, $args) {
  14. if($format === null) {
  15. return "";
  16. }
  17. if($args === null) {
  18. $args = new _hx_array(array());
  19. }
  20. $destString = "";
  21. $argIndex = 0;
  22. $formatIndex = 0;
  23. $percentIndex = 0;
  24. $ch = 0;
  25. $value = null;
  26. $length = 0;
  27. $precision = 0;
  28. $properties = 0;
  29. $fieldCount = 0;
  30. $fieldOutcome = null;
  31. while($formatIndex < strlen($format)) {
  32. $percentIndex = _hx_index_of($format, "%", $formatIndex);
  33. if($percentIndex === -1) {
  34. $destString .= _hx_substr($format, $formatIndex, null);
  35. $formatIndex = strlen($format);
  36. } else {
  37. $destString .= _hx_substr($format, $formatIndex, $percentIndex - $formatIndex);
  38. $fieldOutcome = "** sprintf: invalid format at " . $argIndex . " **";
  39. $length = $properties = $fieldCount = 0;
  40. $precision = -1;
  41. $formatIndex = $percentIndex + 1;
  42. $value = $args[$argIndex++];
  43. while(Std::is($fieldOutcome, _hx_qtype("String")) && $formatIndex < strlen($format)) {
  44. $ch = _hx_char_code_at($format, $formatIndex++);
  45. switch($ch) {
  46. case 35:{
  47. if($fieldCount === 0) {
  48. $properties |= 16;
  49. } else {
  50. $fieldOutcome = "** sprintf: \"#\" came too late **";
  51. }
  52. }break;
  53. case 45:{
  54. if($fieldCount === 0) {
  55. $properties |= 2;
  56. } else {
  57. $fieldOutcome = "** sprintf: \"-\" came too late **";
  58. }
  59. }break;
  60. case 43:{
  61. if($fieldCount === 0) {
  62. $properties |= 4;
  63. } else {
  64. $fieldOutcome = "** sprintf: \"+\" came too late **";
  65. }
  66. }break;
  67. case 32:{
  68. if($fieldCount === 0) {
  69. $properties |= 8;
  70. } else {
  71. $fieldOutcome = "** sprintf: \" \" came too late **";
  72. }
  73. }break;
  74. case 46:{
  75. if($fieldCount < 2) {
  76. $fieldCount = 2;
  77. $precision = 0;
  78. } else {
  79. $fieldOutcome = "** sprintf: \".\" came too late **";
  80. }
  81. }break;
  82. case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:{
  83. if($ch === 48 && $fieldCount === 0) {
  84. $properties |= 1;
  85. } else {
  86. if($fieldCount === 3) {
  87. $fieldOutcome = "** sprintf: shouldn't have a digit after h,l,L **";
  88. } else {
  89. if($fieldCount < 2) {
  90. $fieldCount = 1;
  91. $length = $length * 10 + ($ch - 48);
  92. } else {
  93. $precision = $precision * 10 + ($ch - 48);
  94. }
  95. }
  96. }
  97. }break;
  98. case 100:case 105:{
  99. $fieldOutcome = true;
  100. $destString .= chx_text_Sprintf::formatD($value, $properties, $length, $precision);
  101. }break;
  102. case 111:{
  103. $fieldOutcome = true;
  104. $destString .= chx_text_Sprintf::formatO($value, $properties, $length, $precision);
  105. }break;
  106. case 120:case 88:{
  107. $fieldOutcome = true;
  108. $destString .= chx_text_Sprintf::formatX($value, $properties, $length, $precision, $ch === 88);
  109. }break;
  110. case 101:case 69:{
  111. $fieldOutcome = true;
  112. $destString .= chx_text_Sprintf::formatE($value, $properties, $length, $precision, $ch === 69);
  113. }break;
  114. case 102:{
  115. $fieldOutcome = true;
  116. $destString .= chx_text_Sprintf::formatF($value, $properties, $length, $precision);
  117. }break;
  118. case 103:case 71:{
  119. $fieldOutcome = true;
  120. $destString .= chx_text_Sprintf::formatG($value, $properties, $length, $precision, $ch === 71);
  121. }break;
  122. case 99:case 67:case 115:case 83:{
  123. if($ch === 99 || $ch === 67) {
  124. $precision = 1;
  125. }
  126. $fieldOutcome = true;
  127. $destString .= chx_text_Sprintf::formatS($value, $properties, $length, $precision);
  128. }break;
  129. case 37:{
  130. $fieldOutcome = true;
  131. $destString .= "%";
  132. $argIndex--;
  133. }break;
  134. default:{
  135. $fieldOutcome = "** sprintf: " . Std::string($ch - 48) . " not supported **";
  136. }break;
  137. }
  138. }
  139. if(!_hx_equal($fieldOutcome, true)) {
  140. if(chx_text_Sprintf::$DEBUG) {
  141. $destString .= $fieldOutcome;
  142. }
  143. if(chx_text_Sprintf::$TRACE) {
  144. haxe_Log::trace($fieldOutcome, _hx_anonymous(array("fileName" => "Sprintf.hx", "lineNumber" => 243, "className" => "chx.text.Sprintf", "methodName" => "format")));
  145. }
  146. }
  147. }
  148. }
  149. return $destString;
  150. }
  151. static function finish($output, $value, $properties, $length, $precision, $prefix) {
  152. if($prefix === null) {
  153. $prefix = "";
  154. }
  155. if($prefix === null) {
  156. $prefix = "";
  157. }
  158. if($value < 0) {
  159. $prefix = "-" . $prefix;
  160. } else {
  161. if(($properties & 4) !== 0) {
  162. $prefix = "+" . $prefix;
  163. } else {
  164. if(($properties & 8) !== 0) {
  165. $prefix = " " . $prefix;
  166. }
  167. }
  168. }
  169. if($length === 0 && $precision > -1) {
  170. $length = $precision;
  171. $properties |= 1;
  172. }
  173. while(strlen($output) + strlen($prefix) < $length) {
  174. if(($properties & 2) !== 0) {
  175. $output = $output . " ";
  176. } else {
  177. if(($properties & 1) !== 0) {
  178. $output = "0" . $output;
  179. } else {
  180. $prefix = " " . $prefix;
  181. }
  182. }
  183. }
  184. return $prefix . $output;
  185. }
  186. static function number($v) {
  187. if($v === null) {
  188. return 0.;
  189. }
  190. if(Std::is($v, _hx_qtype("String"))) {
  191. if(_hx_equal($v, "")) {
  192. return 0.0;
  193. }
  194. return Std::parseFloat($v);
  195. }
  196. if(Std::is($v, _hx_qtype("Float"))) {
  197. if(Math::isNaN($v)) {
  198. return $v;
  199. }
  200. return $v;
  201. }
  202. if(Std::is($v, _hx_qtype("Int"))) {
  203. return $v * 1.0;
  204. }
  205. if(Std::is($v, _hx_qtype("Bool"))) {
  206. return (($v) ? 1.0 : 0.0);
  207. }
  208. return Math::$NaN;
  209. }
  210. static function formatD($value, $properties, $length, $precision) {
  211. $output = "";
  212. $value = chx_text_Sprintf::number($value);
  213. if($precision !== 0 || !_hx_equal($value, 0)) {
  214. $output = Std::string(Math::floor(Math::abs($value)));
  215. }
  216. while(strlen($output) < $precision) {
  217. $output = "0" . $output;
  218. }
  219. return chx_text_Sprintf::finish($output, $value, $properties, $length, $precision, null);
  220. }
  221. static function formatO($value, $properties, $length, $precision) {
  222. $output = "";
  223. $prefix = "";
  224. $value = chx_text_Sprintf::number($value);
  225. if($precision !== 0 && !_hx_equal($value, 0)) {
  226. $output = _hx_string_call($value, "toString", array(8));
  227. }
  228. if(($properties & 16) !== 0) {
  229. $prefix = "0";
  230. }
  231. while(strlen($output) < $precision) {
  232. $output = "0" . $output;
  233. }
  234. return chx_text_Sprintf::finish($output, $value, $properties, $length, $precision, $prefix);
  235. }
  236. static function formatX($value, $properties, $length, $precision, $upper) {
  237. $output = "";
  238. $prefix = "";
  239. $value = chx_text_Sprintf::number($value);
  240. if($precision !== 0 && !_hx_equal($value, 0)) {
  241. $output = _hx_string_call($value, "toString", array(16));
  242. }
  243. if(($properties & 16) !== 0) {
  244. $prefix = "0x";
  245. }
  246. while(strlen($output) < $precision) {
  247. $output = "0" . $output;
  248. }
  249. if($upper) {
  250. $prefix = strtoupper($prefix);
  251. $output = strtoupper($output);
  252. } else {
  253. $output = strtolower($output);
  254. }
  255. return chx_text_Sprintf::finish($output, $value, $properties, $length, $precision, $prefix);
  256. }
  257. static function formatE($value, $properties, $length, $precision, $upper) {
  258. $output = "";
  259. $expCount = 0;
  260. $value = chx_text_Sprintf::number($value);
  261. if(Math::abs($value) > 1) {
  262. while(Math::abs($value) > 10) {
  263. $value /= 10;
  264. $expCount++;
  265. }
  266. } else {
  267. while(Math::abs($value) < 1) {
  268. $value *= 10;
  269. $expCount--;
  270. }
  271. }
  272. $expCountStr = chx_text_Sprintf::format("%c%+.2d", new _hx_array(array((($upper) ? "E" : "e"), $expCount)));
  273. if(($properties & 2) !== 0) {
  274. $output = chx_text_Sprintf::formatF($value, $properties, 1, $precision) . $expCountStr;
  275. while(strlen($output) < $length) {
  276. $output .= " ";
  277. }
  278. } else {
  279. $output = chx_text_Sprintf::formatF($value, $properties, intval(Math::max($length - strlen($expCountStr), 0)), $precision) . $expCount;
  280. }
  281. return $output;
  282. }
  283. static function formatF($value, $properties, $length, $precision) {
  284. $output = "";
  285. $intPortion = "";
  286. $decPortion = "";
  287. if($precision === -1) {
  288. $precision = 6;
  289. }
  290. $valStr = Std::string($value);
  291. if(_hx_index_of($valStr, ".", null) === -1) {
  292. $intPortion = Std::string(Math::abs(chx_text_Sprintf::number($valStr)));
  293. $decPortion = "0";
  294. } else {
  295. $intPortion = Std::string(Math::abs(chx_text_Sprintf::number(_hx_substr($valStr, 0, _hx_index_of($valStr, ".", null)))));
  296. $decPortion = _hx_substr($valStr, _hx_index_of($valStr, ".", null) + 1, null);
  297. }
  298. if(_hx_equal(chx_text_Sprintf::number($decPortion), 0)) {
  299. $decPortion = "";
  300. while(strlen($decPortion) < $precision) {
  301. $decPortion .= "0";
  302. }
  303. } else {
  304. if(strlen($decPortion) > $precision) {
  305. $dec = Math::round(Math::pow(10, $precision) * chx_text_Sprintf::number("0." . $decPortion));
  306. if(strlen(Std::string($dec)) > $precision && $dec !== 0) {
  307. $decPortion = "0";
  308. $intPortion = Std::string((Math::abs(chx_text_Sprintf::number($intPortion)) + 1) * (((chx_text_Sprintf::number($intPortion) >= 0) ? 1 : -1)));
  309. } else {
  310. $decPortion = Std::string($dec);
  311. }
  312. }
  313. if(strlen($decPortion) < $precision) {
  314. $decPortion = $decPortion;
  315. while(strlen($decPortion) < $precision) {
  316. $decPortion .= "0";
  317. }
  318. }
  319. }
  320. if($precision === 0) {
  321. $output = $intPortion;
  322. if(($properties & 16) !== 0) {
  323. $output .= ".";
  324. }
  325. } else {
  326. $output = $intPortion . "." . $decPortion;
  327. }
  328. return chx_text_Sprintf::finish($output, Std::parseFloat($valStr), $properties, $length, $precision, "");
  329. }
  330. static function formatG($value, $properties, $length, $precision, $upper) {
  331. $out1 = chx_text_Sprintf::formatE($value, $properties, 1, $precision, $upper);
  332. $out2 = chx_text_Sprintf::formatF($value, $properties, 1, $precision);
  333. if(strlen($out1) < strlen($out2)) {
  334. return chx_text_Sprintf::formatE($value, $properties, $length, $precision, $upper);
  335. } else {
  336. return chx_text_Sprintf::formatF($value, $properties, $length, $precision);
  337. }
  338. }
  339. static function formatS($value, $properties, $length, $precision) {
  340. $output = Std::string($value);
  341. if($precision > 0 && $precision < strlen($output)) {
  342. $output = _hx_substr($output, 0, $precision);
  343. }
  344. $properties &= -30;
  345. return chx_text_Sprintf::finish($output, $value, $properties, $length, $precision, "");
  346. }
  347. function __toString() { return 'chx.text.Sprintf'; }
  348. }