My follow up to the Array extensions is this set of String extension
methods, again, borrowed from other languages. Time for
optimization!
Some methods are still in development - some pretty tricky
checking. I
also need to make _parseParams a bit more robust in allowing multiple
arguments to be passed with the intersections of those strings
being the
result.
----------------------------------------------------------------------
// XString
// Returns a copy of this with the first character converted to
uppercase and the remainder to lowercase.
String.prototype.capitalize = function() {
var a = this.length;
if (a == 0) return "";
var c;
var s = "";
while (--a -(-1)) {
c = this.charCodeAt(a);
if ((c > 64 && c < 91 && a > 0) || (c > 96 && c < 123 &&
a == 0)) c = c ^ 32;
s = String.fromCharCode(c) + s;
}
return s;
};
// If n is greater than the length of str, returns a new String of
// length n with str centered between r; otherwise, returns str.
// If r is not passed, r is a space
String.prototype.center = function(n, r) {
var a = this.length;
if (n > a) {
if (r.length == 0) r = " ";
var d = n - a;
var cl = Math.floor(d / 2);
sl = "";
while (--cl -(-1)) {
sl += r;
}
var cr = Math.ceil(d / 2);
sr = "";
while (--cr -(-1)) {
sr += r;
}
return sl + this + sr;
}
return this;
};
// Returns a new String with the given record separator removed
from the
end of this (if present).
String.prototype.chomp = function(str) {
var n = this.length;
var s = str.length;
if (s == 0) {
if (this.charCodeAt(n - 1) == 13 || this.charCodeAt(n -
1) == 10) {
return this.substring(0, n - 1);
}
} else {
if (this.substring(n - s, n) == str) {
return this.substring(0, n - s);
}
}
return undefined;
};
// Returns a new String with the last character removed. If the string
ends with \r\n,
// both characters are removed. Applying chop to an empty string
returns
an empty string.
// String.chomp is often a safer alternative, as it leaves the string
unchanged if it
// doesn't end in a record separator.
String.prototype.chop = function() {
var n = this.length;
if (n > 0) {
if (this.charCodeAt(n - 1) == 10 && this.charCodeAt(n -
2) == 13) {
return this.substring(0, n - 2);
}
return this.chomp(this.charAt(n - 1));
}
return "";
};
// str parameter defines a set of characters to count. The
intersection
of this set
// defines the characters to count in str. Any str that starts with a
caret (^) is negated.
// The sequence c1--c2 means all characters between c1 and c2.
String.prototype.count = function(str) {
var a = this.length;
if (a == 0) return 0;
var s = 0;
var c;
if (!str.length) return 0;
var o = this._parseParams(str);
if (o == undefined) return undefined;
var p = o.compare;
var except = o.except;
while (--a -(-1)) {
c = this.charAt(a);
if ((p.indexOf(c) == -1) == except) {
s++
}
}
return s;
};
// If n is greater than the length of this, returns a new String of
length n with this
// left justified and r padded; otherwise, returns this. (see
String.center)
String.prototype.ljust = function(n, r) {
var a = this.length;
if (n > a) {
if (!r.length) r = " ";
var d = n - a;
var sr = "";
while (--d -(-1)) {
sr += r;
}
return this + sr;
}
return this;
};
// Returns a copy of str with all characters in the intersection of
its
arguments deleted.
// Uses the same rules for building the set of characters as
String.count
String.prototype.remove = function(str) {
var a = this.length;
if (a == 0) return "";
var s = "";
var c;
if (!str.length) return this;
var o = this._parseParams(str);
if (o == undefined) return undefined;
var p = o.compare;
var except = o.except;
while (--a -(-1)) {
c = this.charAt(a);
if ((p.indexOf(c) == -1) != except) {
s = c + s;
}
}
return s;
};
// Returns a new string with the characters from str in reverse order.
String.prototype.reverse = function() {
return this.split("").reverse().join("");
};
// If n is greater than the length of this, returns a new String of
length n with
// str right justified and r padded; otherwise, returns this (see
String.center)
String.prototype.rjust = function(n, r) {
var a = this.length;
if (n > a) {
if (!r.length) r = " ";
var d = n - a;
var sl = "";
while (--d -(-1)) {
sl += r;
}
return sl + this;
}
return this;
};
// Builds a set of characters from the str parameter using the
procedure
described for
// String.count. Returns a new string where runs of the same character
that occur in this
// set are replaced by a single character. If no arguments are given,
all runs of identical
// characters are replaced by a single character.
String.prototype.squeeze = function(str) {
var a = this.length;
if (a == 0) return "";
var s = "";
var c;
if (!str.length) {
while (--a -(-1)) {
c = this.charAt(a);
if (c != s.charAt(0)) {
s = c + s;
}
}
} else {
var o = this._parseParams(str);
if (o == undefined) return undefined;
var p = o.compare;
var except = o.except;
while (--a -(-1)) {
c = this.charAt(a);
if (c != s.charAt(0) || ((p.indexOf(c) == -1) !=
except)) {
s = c + s;
}
}
}
return s;
};
// Returns a copy of this with leading and trailing whitespace
removed.
String.prototype.strip = function() {
var a = this.length;
if (a == 0) return "";
var f = 0;
var t = a;
var c;
while (--a -(-1)) {
c = this.charCodeAt(a);
if (c < 33) {
t = a;
} else {
break;
}
}
a = this.length;
for (var n = 0; n < a; n++) {
c = this.charCodeAt(n);
if (c < 33) {
f = n;
} else {
f = n;
break;
}
}
return this.substring(f, t);
};
// Returns a copy of str with uppercase alphabetic characters
converted
to
// lowercase and lowercase characters converted to uppercase.
String.prototype.swapcase = function() {
var a = this.length;
if (a == 0) return "";
var c;
var s = "";
while (--a -(-1)) {
c = this.charCodeAt(a);
if ((c > 64 && c < 91) || (c > 96 && c < 123)) c = c ^
32;
s = String.fromCharCode(c) + s;
}
return s;
};
// PRIVATE
// used to parse params for multiple string methods into a string that
is used
// to check against for existence (or not) of characters
String.prototype._parseParams = function(str) {
var o = {};
o.except = false;
var h, g, f, t;
var s = "";
if (str.indexOf("^") == 0 && str.length > 1) {
o.except = true;
str = str.substring(1, str.length);
}
while (str.indexOf("-") > -1) {
h = str.indexOf("-");
g = str.length;
if (h > 0 && g >= 3) {
f = str.charCodeAt(h - 1);
t = str.charCodeAt(h + 1);
str = str.substring(0, h) + str.substring(h + 1,
g);
if (t > f) {
f++;
while (--t -(-1) > f) {
str += String.fromCharCode(t);
}
} else {
t++;
while (--f -(-1) > t) {
str += String.fromCharCode(f);
}
}
} else {
return undefined;
}
}
o.compare = str;
return o;
};
/*
// IN DEVELOPMENT
// Returns the successor to str. The successor is calculated by
incrementing characters starting
// from the rightmost alphanumeric (or the rightmost character if
there
are no alphanumerics) in
// the string. Incrementing a digit always results in another
digit, and
incrementing a letter
// results in another letter of the same case. Incrementing
nonalphanumerics uses the underlying
// character set's collating sequence.
// If the increment generates a ``carry,'' the character to the
left of
it is incremented. This
// process repeats until there is no carry, adding an additional
character if necessary.
String.prototype.succ = function() {
//
};
// Returns a copy of str with the characters in fromString replaced by
the corresponding characters
// in toString. If toString is shorter than fromString, it is padded
with its last character. Both
// strings may use the c1--c2 notation to denote ranges of characters,
and fromString may start with
// a ^, which denotes all characters except those listed.
String.prototype.tr = function(fromStr, toStr) {
var a = this.length;
var fromLen = fromStr.length;
var toLen = toStr.length;
if (a == 0 || fromLen == 0 || toLen == 0) return "";
var s = "";
var c;
var o = this._parseParams(fromStr);
if (o == undefined) return undefined;
var p = o.compare;
var except = o.except;
if (toLen == 1) {
while (--a -(-1)) {
c = this.charAt(a);
if ((p.indexOf(c) == -1) == except) c = toStr;
s = c + s;
}
} else {
// have to figure this one out
}
return s;
};
*/
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com