whitespace
[conkeror.git] / modules / string.js
blob116ebfd3858ed844ab83a94c9fbd3eb4664a850f
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 regex which matches any
33    of them*/
34 function choice_regex (choices) {
35     var regex = "(?:" + choices.map(quotemeta).join("|") + ")";
36     return regex;
40 /**
41  * get_shortdoc_string, given a docstring, returns the portion of the
42  * docstring up to the first newline, or the whole docstring.
43  */
44 function get_shortdoc_string (doc) {
45     var shortdoc = null;
46     if (doc != null) {
47         var idx = doc.indexOf("\n");
48         if (idx >= 0)
49             shortdoc = doc.substring(0,idx);
50         else
51             shortdoc = doc;
52     }
53     return shortdoc;
57 /**
58  * string_format takes a format-string containing %X style format codes,
59  * and an object mapping the code-letters to replacement text.  It
60  * returns a string with the formatting codes replaced by the replacement
61  * text.
62  */
63 function string_format (spec, substitutions) {
64     return spec.replace(/%(.)/g, function (a, b) substitutions[b]);
68 /**
69  * html_escape replaces characters which are special in html with character
70  * entities, safe for inserting as text into an html document.
71  */
72 function html_escape (str) {
73     return str.replace(/&/g, '&')
74         .replace(/</g, '&lt;')
75         .replace(/>/g, '&gt;')
76         .replace(/"/g, '&quot;');
80 /**
81  * get_spaces returns a string of n spaces.
82  */
83 function get_spaces (n) {
84     var x = "";
85     while (x.length < n) x += " ";
86     return x;
90 /**
91  * word_wrap wraps str to line_length.
92  */
93 function word_wrap (str, line_length, line_prefix_first, line_prefix) {
94     if (line_prefix === undefined)
95         line_prefix = line_prefix_first;
96     else if (line_prefix.length < line_prefix_first.length) {
97         line_prefix += get_spaces(line_prefix_first.length - line_prefix.length);
98     }
100     line_length -= line_prefix_first.length;
102     if (line_length < 1)
103         line_length = 1;
105     let cur_prefix = line_prefix_first;
107     var out = "";
108     while (line_length < str.length) {
109         let i = str.lastIndexOf(" ", line_length);
110         if (i == -1)
111             i = str.indexOf(" ", line_length);
112         if (i == -1) {
113             out += cur_prefix + str + "\n";
114             str = "";
115         }
116         else  {
117             out += cur_prefix + str.substr(0, i) + "\n";
118             while (i < str.length && str.charAt(i) ==  " ")
119                 ++i;
120             str = str.substr(i);
121         }
122         cur_prefix = line_prefix;
123     }
124     if (str.length > 0)
125         out += cur_prefix + str + "\n";
126     return out;
131  * or_string joins an array of strings on commas, except for the last
132  * pair, which it joins with the word "or".
133  */
134 function or_string (options) {
135     return options.slice(0,options.length-1)
136         .join(", ") + " or " + options[options.length - 1];
139 provide("string");