]>
Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/Util.java
5 import java
.util
.Arrays
;
9 * @author Charles McGarvey
10 * The TopCoder Arena editor plug-in providing support for Vim.
12 * Distributable under the terms and conditions of the 2-clause BSD license;
13 * see the file COPYING for a complete text of the license.
15 public abstract class Util
18 * Concatenate two arrays into a single array.
19 * @param a First array.
20 * @param b Second array.
21 * @return The combined array.
23 public static <T
> T
[] concat(T
[] a
, T
[] b
)
25 T
[] result
= Arrays
.copyOf(a
, a
.length
+ b
.length
);
26 System
.arraycopy(b
, 0, result
, a
.length
, b
.length
);
31 * Combined string elements from two arrays into a single array, gluing
32 * together elements of the same index with a delimiter string.
33 * @param a First string array.
34 * @param b Second string array.
35 * @param glue The delimiter string.
36 * @return The combined array.
38 public static String
[] combine(String
[] a
, String
[] b
, String glue
)
40 String
[] result
= new String
[Math
.min(a
.length
, b
.length
)];
41 for (int i
= 0; i
< result
.length
; ++i
)
43 result
[i
] = a
[i
] + glue
+ b
[i
];
49 * Join the elements of a string array with a delimiter.
51 * @param glue The delimiter string.
52 * @return The joined string.
54 public static String
join(String
[] a
, String glue
)
56 if (a
.length
== 0) return "";
57 StringBuilder result
= new StringBuilder();
59 for (int i
= 1; i
< a
.length
; ++i
) result
.append(glue
).append(a
[i
]);
60 return result
.toString();
64 * Escape a string by replacing prepending backslashes and double
65 * quotation characters with an extra backslash.
66 * @param The string to be escaped.
67 * @return The escaped string.
69 public static String
escape(String a
)
71 a
= a
.replaceAll("\\\\", "\\\\\\\\");
72 a
= a
.replaceAll("\"", "\\\\\\\"");
77 * Simply read a file's contents into a string object.
78 * @param file The file to read.
79 * @return The contents of the file.
80 * @throws IOException If the file is not readable.
82 public static String
readFile(File file
) throws IOException
84 StringBuilder text
= new StringBuilder();
86 BufferedReader reader
= new BufferedReader(new FileReader(file
.getPath()));
91 while ((line
= reader
.readLine()) != null)
93 text
.append(line
+ System
.getProperty("line.separator"));
101 return text
.toString().replaceAll("\n", System
.getProperty("line.separator"));
105 * Read a resource file into a string object.
106 * The resources should be placed in the directory `resources'
107 * underneath the parent directory of this class. Reading resources
108 * packaged in a jar is allowable.
109 * @param path Relative path to the resource.
110 * @return The contents of the resource.
111 * @throws IOException If the resource is not readable.
113 public static String
readResource(String path
) throws IOException
115 StringBuilder text
= new StringBuilder();
117 InputStream stream
= Util
.class.getResourceAsStream("resources/" + path
);
122 byte[] buffer
= new byte[4096];
124 while (0 < (numBytes
= stream
.read(buffer
)))
126 text
.append(new String(buffer
, 0, numBytes
));
135 return text
.toString().replaceAll("\n", System
.getProperty("line.separator"));
139 * The poor man's template package.
140 * Provide a template and a map of terms to build the result with the
141 * terms expanded into the template. Terms in the template should
142 * appear surrounded with dollar signs. For example, if $MYTERM$
143 * appears in the template, it will be replaced by the value into the
144 * terms map with the key MYTERM (if it exists in the map).
145 * @param template The template string.
146 * @param terms A map of key/value terms.
147 * @return The string expanded from the template and terms.
149 public static String
expandTemplate(String template
, Map
<String
,String
> terms
)
151 String text
= template
;
152 for (String key
: terms
.keySet())
154 text
= text
.replaceAll("\\$" + key
+ "\\$", Util
.escape(terms
.get(key
)));
This page took 0.039244 seconds and 4 git commands to generate.