Make format args explicit in DocString.
Previously it was not clear that passing a single argument to
DocString.text, DocString.append, or DocString.uri would be treated as
a format string. With this change, the 'text', 'append', and 'uri'
methods take literal strings, and the new 'format', 'appendFormat',
and 'formattedUri' methods take format strings.
Bug: 23782192
Change-Id: I9a094575f0831de6659033052305f918c71ac8b7
diff --git a/tools/ahat/src/DocString.java b/tools/ahat/src/DocString.java
index 1d997dc..19666de 100644
--- a/tools/ahat/src/DocString.java
+++ b/tools/ahat/src/DocString.java
@@ -33,11 +33,18 @@
/**
* Construct a new DocString, initialized with the given text.
- * Format arguments are supported.
*/
- public static DocString text(String format, Object... args) {
+ public static DocString text(String str) {
DocString doc = new DocString();
- return doc.append(format, args);
+ return doc.append(str);
+ }
+
+ /**
+ * Construct a new DocString, initialized with the given formatted text.
+ */
+ public static DocString format(String format, Object... args) {
+ DocString doc = new DocString();
+ return doc.appendFormat(format, args);
}
/**
@@ -58,15 +65,22 @@
/**
* Append literal text to the given doc string.
- * Format arguments are supported.
* Returns this object.
*/
- public DocString append(String format, Object... args) {
- String text = String.format(format, args);
+ public DocString append(String text) {
mStringBuilder.append(HtmlEscapers.htmlEscaper().escape(text));
return this;
}
+ /**
+ * Append formatted text to the given doc string.
+ * Returns this object.
+ */
+ public DocString appendFormat(String format, Object... args) {
+ append(String.format(format, args));
+ return this;
+ }
+
public DocString append(DocString str) {
mStringBuilder.append(str.html());
return this;
@@ -101,10 +115,9 @@
/**
* Convenience function for constructing a URI from a string with a uri
- * known to be valid. Format arguments are supported.
+ * known to be valid.
*/
- public static URI uri(String format, Object... args) {
- String uriString = String.format(format, args);
+ public static URI uri(String uriString) {
try {
return new URI(uriString);
} catch (URISyntaxException e) {
@@ -113,6 +126,14 @@
}
/**
+ * Convenience function for constructing a URI from a formatted string with
+ * a uri known to be valid.
+ */
+ public static URI formattedUri(String format, Object... args) {
+ return uri(String.format(format, args));
+ }
+
+ /**
* Render the DocString as html.
*/
public String html() {