Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/platforms/default/lib/string.js
blob: 849d71d4ad89f89736a970d73f78afc1bb71c8d7 (plain)
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
// String additions

String.prototype.forEach = function(block, separator) {
    system.print("WARNING: String.prototype.forEach deprecated");
    block(String(this)); // RHINO bug: it thinks "this" is a Java string (?!)
};

String.prototype.squeeze = function() {
    var set = arguments.length > 0 ? "["+Array.prototype.join.apply(arguments, ["]|["])+"]" : ".|\\n",
        regex = new RegExp("("+set+")\\1+", "g");
    
    return this.replace(regex, "$1");
};

String.prototype.chomp = function(separator) {
    var extra = separator ? separator + "|" : "";
    return this.replace(new RegExp("("+extra+"\\r|\\n|\\r\\n)*$"), "");
};

// Check if the string starts with the given prefix string.
String.prototype.begins = function(str) {
    return this.indexOf(str) === 0;
};

// Check if the string ends with the given postfix string.
String.prototype.ends = function(str) {
    var offset = this.length - str.length;
    return offset >= 0 && this.lastIndexOf(str) === offset;
};

// Trim the string, left/right.
//
// Faster version: http://blog.stevenlevithan.com/archives/faster-trim-javascript
//
// function trim (str) {
// var str = str.replace(/^\s\s*/, ''),
// ws = /\s/,
// i = str.length;
// while (ws.test(str.charAt(--i)));
// return str.slice(0, i + 1);
// }
//Trim the string, left.
var trimBeginExpression = /^\s\s*/g;
String.prototype.trimBegin = function() {
	return this.replace(trimBeginExpression, "");
};

// Trim the string, right.
var trimEndExpression = /\s\s*$/g;
String.prototype.trimEnd = function() {
	return this.replace(trimEndExpression, "");
};

String.prototype.trim = function() {
	return this.replace(trimBeginExpression, "").replace(trimEndExpression, "");
};

/* binary */

// https://wiki.mozilla.org/ServerJS/Binary/B
if (!String.prototype.toByteString)
    String.prototype.toByteString = function(charset) {
        // RHINO bug: it thinks "this" is a Java string (?!)
        var binary = require("binary");
        return new binary.ByteString(String(this), charset);
    };

// https://wiki.mozilla.org/ServerJS/Binary/B
if (!String.prototype.toByteArray)
    String.prototype.toByteArray = function(charset) {
        // RHINO bug: it thinks "this" is a Java string (?!)
        var binary = require("binary");
        return new binary.ByteArray(String(this), charset);
    };

// https://wiki.mozilla.org/ServerJS/Binary/B
if (!String.prototype.charCodes)
    String.prototype.charCodes = function() {
        return Array.prototype.map.call(this, function (c) {
            return c.charCodeAt();
        });
    };

// https://wiki.mozilla.org/ServerJS/Binary/B
if (!String.prototype.fromCharCodes)
    String.fromCharCodes = function (codes) {
        return codes.map(String.fromCharCode).join('');
    };