aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/api/scripting/Formatter.java
blob: 544e70ce4521951c47b8301a96285053e1ec3b91 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.api.scripting;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Formatter is a class to get the type conversion between javascript types and
 * java types for the format (sprintf) method working.
 *
 * <p>In javascript the type for numbers can be different from the format type
 * specifier. For format type '%d', '%o', '%x', '%X' double need to be
 * converted to integer. For format type 'e', 'E', 'f', 'g', 'G', 'a', 'A'
 * integer needs to be converted to double.
 *
 * <p>Format type "%c" and javascript string needs special handling.
 *
 * <p>The javascript date objects can be handled if they are type double (the
 * related javascript code will convert with Date.getTime() to double). So
 * double date types are converted to long.
 *
 * <p>Pattern and the logic for parameter position: java.util.Formatter
 *
 */
final class Formatter {

    private Formatter() {
    }

    /**
     * Method which converts javascript types to java types for the
     * String.format method (jrunscript function sprintf).
     *
     * @param format a format string
     * @param args arguments referenced by the format specifiers in format
     * @return a formatted string
     */
    static String format(final String format, final Object[] args) {
        final Matcher m = FS_PATTERN.matcher(format);
        int positionalParameter = 1;

        while (m.find()) {
            int index = index(m.group(1));
            final boolean previous = isPreviousArgument(m.group(2));
            final char conversion = m.group(6).charAt(0);

            // skip over some formats
            if (index < 0 || previous
                    || conversion == 'n' || conversion == '%') {
                continue;
            }

            // index 0 here means take a positional parameter
            if (index == 0) {
                index = positionalParameter++;
            }

            // out of index, String.format will handle
            if (index > args.length) {
                continue;
            }

            // current argument
            final Object arg = args[index - 1];

            // for date we convert double to long
            if (m.group(5) != null) {
                // convert double to long
                if (arg instanceof Double) {
                    args[index - 1] = ((Double) arg).longValue();
                }
            } else {
                // we have to convert some types
                switch (conversion) {
                    case 'd':
                    case 'o':
                    case 'x':
                    case 'X':
                        if (arg instanceof Double) {
                            // convert double to long
                            args[index - 1] = ((Double) arg).longValue();
                        } else if (arg instanceof String
                                && ((String) arg).length() > 0) {
                            // convert string (first character) to int
                            args[index - 1] = (int) ((String) arg).charAt(0);
                        }
                        break;
                    case 'e':
                    case 'E':
                    case 'f':
                    case 'g':
                    case 'G':
                    case 'a':
                    case 'A':
                        if (arg instanceof Integer) {
                            // convert integer to double
                            args[index - 1] = ((Integer) arg).doubleValue();
                        }
                        break;
                    case 'c':
                        if (arg instanceof Double) {
                            // convert double to integer
                            args[index - 1] = ((Double) arg).intValue();
                        } else if (arg instanceof String
                                && ((String) arg).length() > 0) {
                            // get the first character from string
                            args[index - 1] = (int) ((String) arg).charAt(0);
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        return String.format(format, args);
    }

    /**
     * Method to parse the integer of the argument index.
     *
     * @param s string to parse
     * @return -1 if parsing failed, 0 if string is null, > 0 integer
     */
    private static int index(final String s) {
        int index = -1;

        if (s != null) {
            try {
                index = Integer.parseInt(s.substring(0, s.length() - 1));
            } catch (final NumberFormatException e) {
                //ignored
            }
        } else {
            index = 0;
        }

        return index;
    }

    /**
     * Method to check if a string contains '&lt;'. This is used to find out if
     * previous parameter is used.
     *
     * @param s string to check
     * @return true if '&lt;' is in the string, else false
     */
    private static boolean isPreviousArgument(final String s) {
        return (s != null && s.indexOf('<') >= 0) ? true : false;
    }

    // %[argument_index$][flags][width][.precision][t]conversion
    private static final String formatSpecifier =
            "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";
    // compiled format string
    private static final Pattern FS_PATTERN;

    static {
        FS_PATTERN = Pattern.compile(formatSpecifier);
    }
}