File: format.md

package info (click to toggle)
bnd 5.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 44,092 kB
  • sloc: java: 249,039; xml: 90,727; sh: 655; perl: 153; makefile: 95; python: 47; javascript: 9
file content (117 lines) | stat: -rw-r--r-- 2,534 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
layout: default
class: Macro
title: format ';' STRING (';' ANY )* 
summary: Print a formatted string, automatically converting variables to the specified format if possible.
---

	static String	_format	= "${format;<format>[;args...]}";

	public String _format(String args[]) throws Exception {
		verifyCommand(args, _format, null, 2, Integer.MAX_VALUE);

		Object[] args2 = new Object[args.length + 10];
		
		Matcher m = PRINTF_P.matcher(args[1]);
		int n = 2;
		while ( n < args.length && m.find()) {
			char conversion = m.group(5).charAt(0);
			switch(conversion) {
				// d|f|c|s|h|n|x|X|u|o|z|Z|e|E|g|G|p|\n|%)");
				case 'd':
				case 'u':
				case 'o':
				case 'x':
				case 'X':
				case 'z':
				case 'Z':
					args2[n-2] = Long.parseLong(args[n]);
					n++;
					break;
					
				case 'f':
				case 'e':
				case 'E':
				case 'g':
				case 'G':
				case 'a':
				case 'A':
					args2[n-2] = Double.parseDouble(args[n]);
					n++;
					break;

				case 'c':
					if ( args[n].length() != 1)
						throw new IllegalArgumentException("Character expected but found '"+args[n]+"'");
					args2[n-2] = args[n].charAt(0);
					n++;
					break;
					
				case 'b':
					String v = args[n].toLowerCase();
					if ( v == null || v.equals("false") || v.isEmpty() || (NUMERIC_P.matcher(v).matches() && Double.parseDouble(v)==0.0D))
						args2[n-2] = false;
					else
						args2[n-2] = false;
					n++;
					break;
					
				case 's':
				case 'h':
				case 'H':
				case 'p':
					args2[n-2] = args[n];
					n++;
					break;

				case 't':
				case 'T':
					String dt = args[n];
					
					if ( NUMERIC_P.matcher(dt).matches()) {
						args2[n-2]= Long.parseLong(dt);
					} else {
						DateFormat df;
						switch(args[n].length()) {
							case 6:
								df = new SimpleDateFormat("yyMMdd");
								break;
								
							case 8:
								df = new SimpleDateFormat("yyyyMMdd");
								break;
								
							case 12:
								df = new SimpleDateFormat("yyyyMMddHHmm");
								break;
								
							case 14:
								df = new SimpleDateFormat("yyyyMMddHHmmss");
								break;
							case 19:
								df = new SimpleDateFormat("yyyyMMddHHmmss.SSSZ");
								break;
								
							default:
								throw new IllegalArgumentException("Unknown dateformat " + args[n]);
						}
						args2[n-2] = df.parse(args[n]);
					}	
					break;
					
				case 'n':
				case '%':
					break;
			}
		}

		Formatter f = new Formatter();
		try {
			f.format(args[1], args2);
			return f.toString();
		}
		finally {
			f.close();
		}
	}