[Jspro-cvs] jsPro string.js,1.19,1.20
Brought to you by:
wigleys
|
From: <gat...@us...> - 2003-09-21 22:26:16
|
Update of /cvsroot/jspro/jsPro
In directory sc8-pr-cvs1:/tmp/cvs-serv30463
Modified Files:
string.js
Log Message:
QA: new functions modified for more in-depth error trapping, consistency, and corrections
Index: string.js
===================================================================
RCS file: /cvsroot/jspro/jsPro/string.js,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** string.js 19 Sep 2003 15:46:59 -0000 1.19
--- string.js 21 Sep 2003 22:26:10 -0000 1.20
***************
*** 73,85 ****
/**
! * Concatenate the string representation of <code>vValue</code> to the end of
! * the current string
*
* @summary concatenate
* @author Stuart Wigley
! * @version 1.0, 09/19/03
* @interface <code>String.cat(vValue)</code>
! * @param vValue value to be concatenated to end of string
! * @return concatenated string
* @return <code>null</code> if an exception is encountered
* @throws IllegalArgumentException
--- 73,86 ----
/**
! * Concatenate the string representation of the specified value to the end of
! * this string.
*
* @summary concatenate
* @author Stuart Wigley
! * @author Randolph Fielding
! * @version 1.1, 09/20/03
* @interface <code>String.cat(vValue)</code>
! * @param vValue the value to be concatenated to the end of this string
! * @return a concatenated string
* @return <code>null</code> if an exception is encountered
* @throws IllegalArgumentException
***************
*** 96,102 ****
}
! var sValue = arguments[0].toString();
! var sCatString = this + sValue;
!
}
catch (vError) {
--- 97,101 ----
}
! var sCatString = this + vValue.toString();
}
catch (vError) {
***************
*** 114,123 ****
/**
! * Replaces all Unix, Windows and Macintosh newline characters, tabs and
! * repeated spaces with a single space character.
*
! * @summary compress to spaces
* @author Stuart Wigley
! * @version 1.0, 09/19/03
* @interface <code>String.compress()</code>
* @return a modified string
--- 113,123 ----
/**
! * Replaces all UNIX, Windows and Macintosh newline characters, tabs and
! * repeated spaces in this string with single spaces.
*
! * @summary compress whitespace to single spaces
* @author Stuart Wigley
! * @author Randolph Fielding
! * @version 1.1, 09/21/03
* @interface <code>String.compress()</code>
* @return a modified string
***************
*** 131,137 ****
var vError = null;
var iNumArguments = arguments.length;
- var iDocType = 1;
! if (iNumArguments != 0) {
throw vError = new IllegalArgumentException('String.compress', 0, iNumArguments);
}
--- 131,136 ----
var vError = null;
var iNumArguments = arguments.length;
! if (iNumArguments > 0) {
throw vError = new IllegalArgumentException('String.compress', 0, iNumArguments);
}
***************
*** 324,341 ****
/**
! * Inserts a string into another string at the specified position
*
! * @summary insert a string
* @author Stuart Wigley
! * @version 1.0, 09/19/03
! * @interface <code>String.insert(sInsert)</code>
! * @interface <code>String.insert(sInsert, iStart)</code>
! * @param sInsert the string to be inserted
! * @param iStart the position where the insert should begin
* @return a modified string
* @return <code>null</code> if an exception is encountered
* @throws IllegalArgumentException
*/
! String.prototype.insert = function() {
try {
--- 323,345 ----
/**
! * Inserts the string representation of the specified value at the beginning
! * of this string or at a given position within this string (if the optional
! * start argument is specified).
*
! * @summary insert one string into another
* @author Stuart Wigley
! * @author Randolph Fielding
! * @version 1.1, 09/21/03
! * @interface <code>String.insert(vValue)</code>
! * @interface <code>String.insert(vValue, iStart)</code>
! * @param vValue the value to be inserted into this string
! * @param iStart the index of the character to start at (optional)
* @return a modified string
* @return <code>null</code> if an exception is encountered
+ * @throws ArrayIndexOutOfBoundsException
* @throws IllegalArgumentException
+ * @throws TypeMismatchException
*/
! String.prototype.insert = function(vValue) {
try {
***************
*** 343,375 ****
var vError = null;
var iNumArguments = arguments.length;
- var sInsert = '';
var iStart = 0;
- var iLength = this.length;
! if (iNumArguments == 1) {
! sInsert = arguments[0];
} else if (iNumArguments == 2) {
- sInsert = arguments[0];
iStart = arguments[1];
- } else {
- throw vError = new IllegalArgumentException('String.insert', '1 or 2', iNumArguments);
}
- if (typeof sInsert != 'string') {
- throw vError = new TypeMismatchException('String.insert', 'string', typeof sInsert);
- }
if ((typeof iStart != 'number') || (parseInt(iStart.toString()) != iStart)) {
throw vError = new TypeMismatchException('String.insert', 'number', typeof iStart);
}
var sModifiedString = '';
! for (var i = 0; i < iLength; i++) {
if (i == iStart) {
! sModifiedString += sInsert;
}
sModifiedString += this.charAt(i);
}
-
}
catch (vError) {
--- 347,376 ----
var vError = null;
var iNumArguments = arguments.length;
var iStart = 0;
! if ((iNumArguments < 1) || (iNumArguments > 2)) {
! throw vError = new IllegalArgumentException('String.insert', '1 or 2', iNumArguments);
} else if (iNumArguments == 2) {
iStart = arguments[1];
}
if ((typeof iStart != 'number') || (parseInt(iStart.toString()) != iStart)) {
throw vError = new TypeMismatchException('String.insert', 'number', typeof iStart);
}
+ var iStringLength = this.length;
+
+ if ((iStart < 0) || (iStart >= iStringLength)) {
+ throw vError = new ArrayIndexOutOfBoundsException('String.insert', iStart, iStringLength);
+ }
+
var sModifiedString = '';
! for (var i = 0; i < iStringLength; i++) {
if (i == iStart) {
! sModifiedString += vValue.toString();
}
sModifiedString += this.charAt(i);
}
}
catch (vError) {
***************
*** 624,628 ****
/**
! * Replaces all Unix, Windows and Macintosh newline characters in this string
* with either HTML <BR> elements or XHTML <br /> elements.
*
--- 625,629 ----
/**
! * Replaces all UNIX, Windows and Macintosh newline characters in this string
* with either HTML <BR> elements or XHTML <br /> elements.
*
***************
*** 680,697 ****
/**
! * Overlays a string over another string at the specified position
*
! * @summary overlay a string
* @author Stuart Wigley
! * @version 1.0, 09/19/03
! * @interface <code>String.overlay(sInsert)</code>
! * @interface <code>String.overlay(sInsert, iStart)</code>
! * @param sOverlay the string to be overlayed
! * @param iStart the position where the overlay should begin
* @return a modified string
* @return <code>null</code> if an exception is encountered
* @throws IllegalArgumentException
*/
! String.prototype.overlay = function() {
try {
--- 681,703 ----
/**
! * Overlays the string representation of the specified value over the beginning
! * of this string or over a later portion of this string (if the optional start
! * argument is specified).
*
! * @summary overlay one string over another
* @author Stuart Wigley
! * @author Randolph Fielding
! * @version 1.1, 09/21/03
! * @interface <code>String.overlay(vValue)</code>
! * @interface <code>String.overlay(vValue, iStart)</code>
! * @param vValue the value to be overlayed over this string
! * @param iStart the index of the character to start at (optional)
* @return a modified string
* @return <code>null</code> if an exception is encountered
+ * @throws ArrayIndexOutOfBoundsException
* @throws IllegalArgumentException
+ * @throws TypeMismatchException
*/
! String.prototype.overlay = function(vValue) {
try {
***************
*** 699,732 ****
var vError = null;
var iNumArguments = arguments.length;
- var sOverlay = '';
var iStart = 0;
- var iLength = this.length;
! if (iNumArguments == 1) {
! sOverlay = arguments[0];
} else if (iNumArguments == 2) {
- sOverlay = arguments[0];
iStart = arguments[1];
- } else {
- throw vError = new IllegalArgumentException('String.overlay', '1 or 2', iNumArguments);
}
- if (typeof sOverlay != 'string') {
- throw vError = new TypeMismatchException('String.insert', 'string', typeof sOverlay);
- }
if ((typeof iStart != 'number') || (parseInt(iStart.toString()) != iStart)) {
! throw vError = new TypeMismatchException('String.insert', 'number', typeof iStart);
}
var sModifiedString = '';
! for (var i = 0; i < iLength; i++) {
if (i == iStart) {
! sModifiedString += sOverlay;
! i += sOverlay.length;
}
sModifiedString += this.charAt(i);
}
-
}
catch (vError) {
--- 705,739 ----
var vError = null;
var iNumArguments = arguments.length;
var iStart = 0;
! if ((iNumArguments < 1) || (iNumArguments > 2)) {
! throw vError = new IllegalArgumentException('String.overlay', '1 or 2', iNumArguments);
} else if (iNumArguments == 2) {
iStart = arguments[1];
}
if ((typeof iStart != 'number') || (parseInt(iStart.toString()) != iStart)) {
! throw vError = new TypeMismatchException('String.overlay', 'number', typeof iStart);
! }
!
! var iStringLength = this.length;
!
! if ((iStart < 0) || (iStart >= iStringLength)) {
! throw vError = new ArrayIndexOutOfBoundsException('String.overlay', iStart, iStringLength);
}
var sModifiedString = '';
! for (var i = 0; i < iStringLength; i++) {
if (i == iStart) {
! var sValue = vValue.toString();
! sModifiedString += sValue;
! i += sValue.length;
! if (i >= iStringLength) {
! break;
! }
}
sModifiedString += this.charAt(i);
}
}
catch (vError) {
***************
*** 821,838 ****
/**
! * Removes all characters from the string or a subset of characters (if the
! * optional start and end arguments are specified)
*
! * @summary delete characters
* @author Stuart Wigley
! * @version 1.0, 09/19/03
* @interface <code>String.remove()</code>
* @interface <code>String.remove(iStart)</code>
! * @interface <code>String.remove(iStart, iEnd)</code>
! * @param iStart the string position to start at (optional)
! * @param iEnd the string position to end at (optional)
! * @return the modified string
* @return <code>null</code> if an exception is encountered
* @throws IllegalArgumentException
*/
String.prototype.remove = function() {
--- 828,849 ----
/**
! * Removes all characters from this string or a subset of characters from this
! * string (if the optional start and length arguments are specified).
*
! * @summary remove characters
* @author Stuart Wigley
! * @author Randolph Fielding
! * @version 1.1, 09/21/03
* @interface <code>String.remove()</code>
* @interface <code>String.remove(iStart)</code>
! * @interface <code>String.remove(iStart, iLength)</code>
! * @param iStart the index of the character to start at (optional)
! * @param iLength the number of characters to remove beginning at
! * <code>iStart</code> (optional)
! * @return a modified string
* @return <code>null</code> if an exception is encountered
+ * @throws ArrayIndexOutOfBoundsException
* @throws IllegalArgumentException
+ * @throws TypeMismatchException
*/
String.prototype.remove = function() {
***************
*** 841,847 ****
var vError = null;
var iNumArguments = arguments.length;
var iStart = 0;
! var iEnd = this.length + 1;
if (iNumArguments > 2) {
--- 852,859 ----
var vError = null;
+ var iStringLength = this.length;
var iNumArguments = arguments.length;
var iStart = 0;
! var iLength = iStringLength;
if (iNumArguments > 2) {
***************
*** 849,855 ****
} else if (iNumArguments == 2) {
iStart = arguments[0];
! iEnd = arguments[1];
} else if (iNumArguments == 1) {
iStart = arguments[0];
}
--- 861,868 ----
} else if (iNumArguments == 2) {
iStart = arguments[0];
! iLength = arguments[1];
} else if (iNumArguments == 1) {
iStart = arguments[0];
+ iLength -= iStart;
}
***************
*** 858,868 ****
}
! if ((typeof iEnd != 'number') || (parseInt(iEnd.toString()) != iEnd)) {
! throw vError = new TypeMismatchException('String.remove', 'number', typeof iEnd);
}
! var sSubString = this.slice(iStart, iEnd);
! var oRegExp = new RegExp(sSubString);
! var sDeleted = this.replace(oRegExp, '');
}
catch (vError) {
--- 871,893 ----
}
! if ((typeof iLength != 'number') || (parseInt(iLength.toString()) != iLength)) {
! throw vError = new TypeMismatchException('String.remove', 'number', typeof iLength);
}
! var iEnd = iStart + iLength;
!
! if (iStart < 0) {
! throw vError = new ArrayIndexOutOfBoundsException('String.remove', iStart, iStringLength);
! }
!
! if (iLength <= 0) {
! throw vError = new ArrayIndexOutOfBoundsException('String.remove', iLength, iStringLength);
! }
!
! if (iEnd > iStringLength) {
! throw vError = new ArrayIndexOutOfBoundsException('String.remove', iEnd, iStringLength);
! }
!
! var sModifiedString = this.replace(new RegExp(this.slice(iStart, iEnd)), '');
}
catch (vError) {
***************
*** 874,878 ****
finally {
! return vError ? null : sDeleted;
}
}
--- 899,903 ----
finally {
! return vError ? null : sModifiedString;
}
}
***************
*** 928,939 ****
/**
! * Repeats this string a specified number of times.
*
* @summary repeat characters
* @author Stuart Wigley
* @author Randolph Fielding
! * @version 1.1, 08/04/03
! * @interface <code>String.repeat(iMultiplier)</code>
! * @param iMultiplier the number of times to repeat this string
* @return a new string
* @return <code>null</code> if an exception is encountered
--- 953,966 ----
/**
! * Repeats each character in this string, in succession, a specified number of
! * times.
*
* @summary repeat characters
* @author Stuart Wigley
* @author Randolph Fielding
! * @version 1.1, 09/21/03
! * @interface <code>String.repeatChars(iMultiplier)</code>
! * @param iMultiplier the number of times to repeat each character in this
! * string
* @return a new string
* @return <code>null</code> if an exception is encountered
***************
*** 956,963 ****
}
var sRepeatedString = '';
- var iLength = this.length;
! for (var i = 0; i < iLength; i++) {
for (var j = 0; j < iMultiplier; j++) {
sRepeatedString += this.charAt(i);
--- 983,990 ----
}
+ var iStringLength = this.length;
var sRepeatedString = '';
! for (var i = 0; i < iStringLength; i++) {
for (var j = 0; j < iMultiplier; j++) {
sRepeatedString += this.charAt(i);
***************
*** 1242,1250 ****
/**
! * Swap every second character in the string
*
* @summary swap characters
* @author Stuart Wigley
! * @version 1.0, 09/19/03
* @interface <code>String.swap()</code>
* @return a modified string
--- 1269,1279 ----
/**
! * Swaps every second character in this string with the character immediately
! * preceding it.
*
* @summary swap characters
* @author Stuart Wigley
! * @author Randolph Fielding
! * @version 1.1, 09/21/03
* @interface <code>String.swap()</code>
* @return a modified string
***************
*** 1263,1271 ****
}
var sModifiedString = '';
- var iLength = this.length;
! for (var i = 0; i < iLength; i += 2) {
! sModifiedString += this.charAt(i + 1);
sModifiedString += this.charAt(i);
}
--- 1292,1302 ----
}
+ var iStringLength = this.length;
var sModifiedString = '';
! for (var i = 0; i < iStringLength; i += 2) {
! if ((i + 1) < iStringLength) {
! sModifiedString += this.charAt(i + 1);
! }
sModifiedString += this.charAt(i);
}
***************
*** 1341,1361 ****
/**
! * Truncate a string to the specified length. If specified the final three
! * characters are replaced with an ellipsis.
*
* @summary truncate
* @author Stuart Wigley
! * @version 1.0, 09/03/03
* @interface <code>String.truncate(iLength)</code>
! * @interface <code>String.truncate(iEllipsis)</code>
! * @param iLength the position at which truncation should occur
! * @param iEllipsis replace the final three characters with an ellipsis.
! * (yes: 1; no: 0; default: 0) (optional)
* @return a modified string
* @return <code>null</code> if an exception is encountered
* @throws IllegalArgumentException
* @throws TypeMismatchException
*/
! String.prototype.truncate = function() {
try {
--- 1372,1399 ----
/**
! * Truncates this string to the specified length. If specified, the final three
! * characters of the truncated string are replaced with an ellipsis.
*
* @summary truncate
* @author Stuart Wigley
! * @author Randolph Fielding
! * @version 1.1, 09/21/03
* @interface <code>String.truncate(iLength)</code>
! * @interface <code>String.truncate(iLength, bUseEllipsis)</code>
! * @param iLength the index of the character to truncate at
! * @param bUseEllipsis a boolean value representing whether the final three
! * characters should be replaced with an ellipsis or not
! * (optional)
! * @return the original string if (1) <code>iLength</code> is
! * greater than or equal to the length of the original
! * string or (2) <code>iLength <= 3</code> and
! * <code>bUseEllipsis = true</code>
* @return a modified string
* @return <code>null</code> if an exception is encountered
+ * @throws ArrayIndexOutOfBoundsException
* @throws IllegalArgumentException
* @throws TypeMismatchException
*/
! String.prototype.truncate = function(iLength) {
try {
***************
*** 1363,1376 ****
var vError = null;
var iNumArguments = arguments.length;
! var iLength = this.length;
! var iEllipsis = 0;
! if (iNumArguments == 1) {
! iLength = arguments[0];
! } else if (iNumArguments == 2) {
! iLength = arguments[0];
! iEllipsis = arguments[1];
! } else {
throw vError = new IllegalArgumentException('String.truncate', '1 or 2', iNumArguments);
}
--- 1401,1410 ----
var vError = null;
var iNumArguments = arguments.length;
! var bUseEllipsis = false;
! if ((iNumArguments < 1) || (iNumArguments > 2)) {
throw vError = new IllegalArgumentException('String.truncate', '1 or 2', iNumArguments);
+ } else if (iNumArguments == 2) {
+ bUseEllipsis = arguments[1];
}
***************
*** 1379,1389 ****
}
var sTruncatedString = '';
! for (var i = 0; i < iLength; i++) {
! if ((iEllipsis == 1) && (iLength - i < 4)) {
! sTruncatedString += '.';
! } else {
! sTruncatedString += this.charAt(i);
}
}
--- 1413,1433 ----
}
+ if (typeof bUseEllipsis != 'boolean') {
+ throw vError = new TypeMismatchException('String.truncate', 'boolean', typeof bUseEllipsis);
+ }
+
+ var iStringLength = this.length;
+
+ if (iLength < 0) {
+ throw vError = new ArrayIndexOutOfBoundsException('String.truncate', iLength, iStringLength);
+ }
+
var sTruncatedString = '';
! if ((iLength >= iStringLength) || (bUseEllipsis && (iLength <= 3))) {
! sTruncatedString = this;
! } else {
! for (var i = 0; i < iLength; i++) {
! sTruncatedString += (bUseEllipsis && ((iLength - i) <= 3)) ? '.' : this.charAt(i);
}
}
|