--- /dev/null
+/*\r
+ Copyright 2013 Daniel Wirtz <dcode@dcode.io>\r
+\r
+ Licensed under the Apache License, Version 2.0 (the "License");\r
+ you may not use this file except in compliance with the License.\r
+ You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+ */\r
+var ProtoBuf = require("../../index.js");\r
+\r
+/**\r
+ * Utility namespace.\r
+ * @exports pbjs/util\r
+ * @namespace\r
+ */\r
+var util = module.exports = {};\r
+\r
+/**\r
+ * Extracts builder options with the specified prefix from a set of CLI options.\r
+ * @param {!Object.<string,*>} options CLI options\r
+ * @param {string} prefix Prefix\r
+ * @returns {!Object.<string,*>}\r
+ */\r
+util.getBuilderOptions = function(options, prefix) {\r
+ if (!options[prefix])\r
+ return {};\r
+ var builderOptions = {};\r
+ options[prefix].forEach(function(kv) {\r
+ var key, val;\r
+ var p = kv.indexOf("=");\r
+ if (p < 0) {\r
+ key = kv;\r
+ val = true;\r
+ } else {\r
+ key = kv.substring(0, p);\r
+ val = kv.substring(p+1);\r
+ if (val === "true")\r
+ val = true;\r
+ else if (val === "false")\r
+ val = false;\r
+ else {\r
+ var intval = parseInt(val, 10);\r
+ if (intval == val)\r
+ val = intval;\r
+ }\r
+ }\r
+ builderOptions[key] = val;\r
+ });\r
+ return builderOptions;\r
+};\r
+\r
+/**\r
+ * Pads a string to the specified length.\r
+ * @param {string} str String to pad\r
+ * @param {number} len Pad length\r
+ * @param {boolean=} left Whether to pad to the left, defaults to `false`\r
+ * @returns {string}\r
+ */\r
+util.pad = function(str, len, left) {\r
+ while (str.length < len)\r
+ left ? str = " "+str : str += " ";\r
+ return str;\r
+};\r
+\r
+/**\r
+ * Indents a string by the specified whitespace.\r
+ * @param {string} str String to indent\r
+ * @param {string|number} ws Whitespace string or number of whitespaces\r
+ * @returns {string}\r
+ */\r
+util.indent = function(str, ws) {\r
+ if (ws === 0 || ws === "")\r
+ return str;\r
+ var lines = str.split(/\r?\n/);\r
+ if (typeof ws === 'number') {\r
+ var n = ws; ws = "";\r
+ while (ws.length < n) ws += " ";\r
+ }\r
+ for (var i=1; i<lines.length; ++i)\r
+ lines[i] = ws+lines[i];\r
+ return lines.join("\n");\r
+};\r
+\r
+/**\r
+ * Extends an object with additional properties.\r
+ * @param {!Object.<string,*>} subject Subject to extend\r
+ * @param {!Object.<string,*>} extension Extensions to apply\r
+ */\r
+util.extend = function(subject, extension) {\r
+ Object.keys(extension).forEach(function(key) {\r
+ subject[key] = extension[key];\r
+ });\r
+};\r
+\r
+/**\r
+ * Groups extensions by extended message.\r
+ * @param {!ProtoBuf.Reflect.Namespace} ns Namespace\r
+ * @returns {?Object.<string,!Array.<!ProtoBuf.Reflect.Message.ExtensionField>>}\r
+ */\r
+util.groupExtensions = function(ns) {\r
+ var exts = {},\r
+ n = 0;\r
+ ns.getChildren(ProtoBuf.Reflect.Extension).forEach(function(ext) {\r
+ var msg = ext.field.parent,\r
+ fqn = msg.fqn();\r
+ if (!exts[fqn])\r
+ exts[fqn] = [];\r
+ exts[fqn].push(ext.field);\r
+ n++;\r
+ });\r
+ return n > 0 ? exts : null;\r
+};\r
+\r
+/**\r
+ * Tests if the specified import name is referencing an internal descriptor.\r
+ * @param {string} name Import name\r
+ * @returns {boolean}\r
+ */\r
+util.isDescriptor = function(name) {\r
+ return /^google\/protobuf\/descriptor/.test(name);\r
+};
\ No newline at end of file