choice_regex renamed to choice_regexp
[conkeror.git] / modules / string.js
bloba8c1a0f9e5519be92c640bc08db1a3122b125861
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 in_module(null);
12 /**
13  * trim_whitespace removes whitespace from the beginning and end of the
14  * given string.
15  */
16 function trim_whitespace (str) {
17     return (new String(str)).replace(/^\s+|\s+$/g, "");
21 function shell_quote (str) {
22     var s = str.replace("\"", "\\\"", "g");
23     s = s.replace("$", "\$", "g");
24     return s;
27 /* Like perl's quotemeta. Backslash all non-alphanumerics. */
28 function quotemeta (str) {
29     return str.replace(/([^a-zA-Z0-9])/g, "\\$1");
32 /* Given a list of choices (strings), return a regexp which matches any
33    of them*/
34 function choice_regexp (choices) {
35     return ("(?:" + choices.map(quotemeta).join("|") + ")");
39 /**
40  * get_shortdoc_string, given a docstring, returns the portion of the
41  * docstring up to the first newline, or the whole docstring.
42  */
43 function get_shortdoc_string (doc) {
44     var shortdoc = null;
45     if (doc != null) {
46         var idx = doc.indexOf("\n");
47         if (idx >= 0)
48             shortdoc = doc.substring(0,idx);
49         else
50             shortdoc = doc;
51     }
52     return shortdoc;
56 /**
57  * string_format takes a format-string containing %X style format codes,
58  * and an object mapping the code-letters to replacement text.  It
59  * returns a string with the formatting codes replaced by the replacement
60  * text.
61  */
62 function string_format (spec, substitutions) {
63     return spec.replace(/%(.)/g, function (a, b) substitutions[b]);
67 /**
68  * html_escape replaces characters which are special in html with character
69  * entities, safe for inserting as text into an html document.
70  */
71 function html_escape (str) {
72     return str.replace(/&/g, '&')
73         .replace(/</g, '&lt;')
74         .replace(/>/g, '&gt;')
75         .replace(/"/g, '&quot;');
79 /**
80  * get_spaces returns a string of n spaces.
81  */
82 function get_spaces (n) {
83     var x = "";
84     while (x.length < n) x += " ";
85     return x;
89 /**
90  * word_wrap wraps str to line_length.
91  */
92 function word_wrap (str, line_length, line_prefix_first, line_prefix) {
93     if (line_prefix === undefined)
94         line_prefix = line_prefix_first;
95     else if (line_prefix.length < line_prefix_first.length) {
96         line_prefix += get_spaces(line_prefix_first.length - line_prefix.length);
97     }
99     line_length -= line_prefix_first.length;
101     if (line_length < 1)
102         line_length = 1;
104     let cur_prefix = line_prefix_first;
106     var out = "";
107     while (line_length < str.length) {
108         let i = str.lastIndexOf(" ", line_length);
109         if (i == -1)
110             i = str.indexOf(" ", line_length);
111         if (i == -1) {
112             out += cur_prefix + str + "\n";
113             str = "";
114         }
115         else  {
116             out += cur_prefix + str.substr(0, i) + "\n";
117             while (i < str.length && str.charAt(i) ==  " ")
118                 ++i;
119             str = str.substr(i);
120         }
121         cur_prefix = line_prefix;
122     }
123     if (str.length > 0)
124         out += cur_prefix + str + "\n";
125     return out;
130  * or_string joins an array of strings on commas, except for the last
131  * pair, which it joins with the word "or".
132  */
133 function or_string (options) {
134     return options.slice(0,options.length-1)
135         .join(", ") + " or " + options[options.length - 1];
138 provide("string");