mime-type-override: use js object for table instead of string_hashmap
[conkeror.git] / modules / string.js
blob778f4d9743c222f9bc95e399d2ea1353f588f183
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 /**
11  * trim_whitespace removes whitespace from the beginning and end of the
12  * given string.
13  */
14 function trim_whitespace (str) {
15     return (new String(str)).replace(/^\s+|\s+$/g, "");
19 function shell_quote (str) {
20     var s = str.replace("\"", "\\\"", "g");
21     s = s.replace("$", "\$", "g");
22     return s;
25 /* Like perl's quotemeta. Backslash all non-alphanumerics. */
26 function quotemeta (str) {
27     return str.replace(/([^a-zA-Z0-9])/g, "\\$1");
30 /* Given a list of choices (strings), return a regexp which matches any
31    of them*/
32 function choice_regexp (choices) {
33     return ("(?:" + choices.map(quotemeta).join("|") + ")");
37 /**
38  * get_shortdoc_string, given a docstring, returns the portion of the
39  * docstring up to the first newline, or the whole docstring.
40  */
41 function get_shortdoc_string (doc) {
42     var shortdoc = null;
43     if (doc != null) {
44         var idx = doc.indexOf("\n");
45         if (idx >= 0)
46             shortdoc = doc.substring(0,idx);
47         else
48             shortdoc = doc;
49     }
50     return shortdoc;
54 /**
55  * string_format takes a format-string containing %X style format codes,
56  * and an object mapping the code-letters to replacement text.  It
57  * returns a string with the formatting codes replaced by the replacement
58  * text.
59  */
60 function string_format (spec, substitutions) {
61     return spec.replace(/%(.)/g, function (a, b) substitutions[b]);
65 /**
66  * html_escape replaces characters which are special in html with character
67  * entities, safe for inserting as text into an html document.
68  */
69 function html_escape (str) {
70     return str.replace(/&/g, '&')
71         .replace(/</g, '&lt;')
72         .replace(/>/g, '&gt;')
73         .replace(/"/g, '&quot;');
77 /**
78  * get_spaces returns a string of n spaces.
79  */
80 function get_spaces (n) {
81     var x = "";
82     while (x.length < n) x += " ";
83     return x;
87 /**
88  * word_wrap wraps str to line_length.
89  */
90 function word_wrap (str, line_length, line_prefix_first, line_prefix) {
91     if (line_prefix === undefined)
92         line_prefix = line_prefix_first;
93     else if (line_prefix.length < line_prefix_first.length) {
94         line_prefix += get_spaces(line_prefix_first.length - line_prefix.length);
95     }
97     line_length -= line_prefix_first.length;
99     if (line_length < 1)
100         line_length = 1;
102     let cur_prefix = line_prefix_first;
104     var out = "";
105     while (line_length < str.length) {
106         let i = str.lastIndexOf(" ", line_length);
107         if (i == -1)
108             i = str.indexOf(" ", line_length);
109         if (i == -1) {
110             out += cur_prefix + str + "\n";
111             str = "";
112         }
113         else  {
114             out += cur_prefix + str.substr(0, i) + "\n";
115             while (i < str.length && str.charAt(i) ==  " ")
116                 ++i;
117             str = str.substr(i);
118         }
119         cur_prefix = line_prefix;
120     }
121     if (str.length > 0)
122         out += cur_prefix + str + "\n";
123     return out;
128  * or_string joins an array of strings on commas, except for the last
129  * pair, which it joins with the word "or".
130  */
131 function or_string (options) {
132     return options.slice(0,options.length-1)
133         .join(", ") + " or " + options[options.length - 1];
136 provide("string");