-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathString.js
165 lines (163 loc) · 5.47 KB
/
String.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Non-standards from Firefox
if (!String.prototype.quote) String.prototype.quote = function() {
// return JSON.stringify(this).substr(1,-1);
var s="";
for (var i=0; i<this.length; i++) {
var c = this.charCodeAt(i);
if (c > 31 && c < 127) {
if (c == 34 || c == 39 || c == 92)
s += "\\"; /* for ", ' and \ */
s += this.charAt(i);
} else {
s += "\\";
if (c == 0)
s += "0";
else if (c == 8)
s += "b";
else if (c == 9)
s += "t";
else if (c == 10)
s += "n";
else if (c == 11)
s += "v";
else if (c == 12)
s += "f";
else if (c == 13)
s += "r";
else
s += "u"+c.toString(16).padleft(4, "0");
}
}
return s;
};
if (!String.prototype.trimRight) String.prototype.trimRight = function () {
/* Trims whitespace from the end of the string */
return this.replace(/\s+$/,'');
};
if (!String.prototype.trimLeft) String.prototype.trimLeft = function () {
/* Trims whitespace from the beginning of the string */
return this.replace(/^\s+/,'');
};
// Own implementations
String.prototype.bin = function bin() {
return this.charAt(0) == "1"
? -parseInt(this.substr(1).replace(/[01]/g, function(d){return +!+d;}), 2)-1
: parseInt(this.substr(1), 2);
};
String.prototype.hex = function hex() {
return parseInt(this, 16);
};
String.prototype.dez = function dez() {
alert("deprecated: String.dez!");
throw new Error();
return parseInt(this, 16); // sic!
};
String.prototype.padleft = function padleft(len, cha) {
cha = String(cha) || " ";
return (new Array(1+~~(len/cha.length)).join(cha) + this).substr(-len);
};
String.prototype.padright = function padright(len, cha) {
cha = String(cha) || " ";
return (this + new Array(1+~~(len/cha.length)).join(cha)).substr(0, len);
};
String.prototype.repeat = function repeat(times) {
for(var r = ""; --times >= 0; r+=this);
return r;
};
String.prototype.indent = function indent(t, i) {
t = t || "\t";
if (typeof i == "number")
t = (""+t).repeat(i);
return t+this.split("\n").join("\n"+t);
};
String.prototype.splitreg = function splitreg(expr, times) {
var erg =[], ze, i=0;
if (typeof expr=='string') {
expr = new RegExp("(.*?)("+expr+")","g");
times = times || Number.POSITIVE_INFINITY;
} else times = times || 1000;
while((ze = expr.exec(this)) && i < times) {
erg.push(ze[1]);
erg.push(ze[2]);
i++;
}
erg.input = this;
return erg.trim();
};
String.prototype.reverse = function reverse() {
return this.split("").reverse().join("");
};
String.prototype.splice = function splice(index, length, replace) {
return this.substring(0, index) + Array.prototype.slice.call(arguments, 2).join("") + this.substring(index);
};
String.prototype.spn = function spn(s) {
/* Ermittelt die Länge der am Anfang übereinstimmenden Zeichen, PHP: strspn() */
return this.match(new RegExp("^["+s+"]*"))[0].length;
};
String.prototype.endspn = function endspn(s, e) {
/* Ermittelt die Länge der am Ende übereinstimmenden Zeichen */
// return this.reverse().spn(s);
return this.match(new RegExp("["+s+"]*$"))[0].length;
};
String.prototype.startsWith = String.prototype.beginsWith = function beginsWith(s) {
return this.substring(0, s.length) === s;
};
String.prototype.endsWith = function endsWith(s) {
return this.substr(-s.length) === s;
};
String.prototype.contains = function contains(s) {
return this.indexOf(s) != -1;
};
String.prototype.replaceChars = function replaceChars(map) {
var i, reg = "";
for (i in map)
reg += i;
return this.replace(new RegExp("["+reg.replace(/(\]|-)/,"\\$1")+"]",'g'), function(char) { return map[char]; });
};
/* String.specialChars = {
'\b': "\\b",
'\t': "\\t",
'\n': "\\n",
'\f': "\\f",
'\r': "\\r",
'\\': "\\\\"
} */
String.prototype.ucFirst = function ucFirst() {
return this.replace(/^./, function(x) { return x.toUpperCase(); });
};
String.prototype.rot13 = function rot13() {
return this.replace(/[A-Za-z]/g, function(char) {
var code=char.charCodeAt(0);
if (code < 78) return String.fromCharCode(code + 13); //65-77: A-M
if (code < 91) return String.fromCharCode(code - 13); //78-90: N-Z
if (code < 110) return String.fromCharCode(code + 13); //97-109: a-m
return String.fromCharCode(code - 13); //110-122: n-z
// start + ( code - start + 13) % 26
});
};
String.prototype.rescape = String.prototype.regExp = function regExp(save) {
return this.replace(save
? /[\\+*?\[^\]$(){}=!<>|:-]/g // PHP: PRCE preg_quote =!<> dürften in JS unerheblich sein, / wird von new RegExp() maskiert
: /[{}()[\]\\.?*+^$|=!:~-]/g //-> {}()[\]\\.?*+^$|=!:~- // Bergi
/*/[.*+?^=!:${}()|[\]\/\\]/g -> {}()[\]\\.?*+^$|=!:\/ // Prototype 1.7
/[-.*+?^${}()|[\]\/\\]/g -> {}()[\]\\.?*+^$|- // MooTools
/[{}()|.?*+^$\[\]\\\/]/g -> {}()[\]\\.?*+^$|\/ // Codeispoetry, Umherirrender
/[.?*+^$[\]\\(){}-]/g -> {}()[\]\\.?*+^$- // http://stackoverflow.com/questions/2593637/how-to-escape-regular-expression-in-javascript
/[\\{}()|.?*+\-^$\[\]]/g -> {}()[\]\\.?*+^$|- // /1.17wmf1/resources/mediawiki/mediawiki.js (kopiert von jQuery)
/[\/()[\]{}|*+-.,^$?\\]/g -> {}()[\]\\.?*+^$|-,\/ // base2
*/
, "\\$&");
};
String.prototype.mask = function mask(chars, maske) {
return this.replace(new RegExp("["+chars+"]","g"), (maske || "\\")+"$&"); //chars maskieren?
};
/* STRING-PART TEST *
var s = "abcd",
m = [-5,-4,-3,-2,-1,0,1,2,3,4,5,undefined];
return Array.kartesischesProdukt(m, m).map(function(x) {
return "("+(x[0]||"u")+","+(x[1]||"u")+"): \t" +
[s.substr, s.substring, s.slice].map(function(y) {
return y.apply(s,x);
}).join("\t");
}).join("\n");
*/