michael@0: /* michael@0: Copyright (c) 2006 Lawrence Oluyede michael@0: michael@0: Permission is hereby granted, free of charge, to any person obtaining a copy michael@0: of this software and associated documentation files (the "Software"), to deal michael@0: in the Software without restriction, including without limitation the rights michael@0: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell michael@0: copies of the Software, and to permit persons to whom the Software is michael@0: furnished to do so, subject to the following conditions: michael@0: michael@0: The above copyright notice and this permission notice shall be included in all michael@0: copies or substantial portions of the Software. michael@0: michael@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE michael@0: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER michael@0: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, michael@0: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE michael@0: SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: startsWith(str, prefix[, start[, end]]) -> bool michael@0: michael@0: Return true if str ends with the specified prefix, false otherwise. michael@0: With optional start, test str beginning at that position. michael@0: With optional end, stop comparing str at that position. michael@0: prefix can also be an array of strings to try. michael@0: */ michael@0: michael@0: var EXPORTED_SYMBOLS = ['startsWith', 'endsWith']; michael@0: michael@0: function startsWith(str, prefix, start, end) { michael@0: if (arguments.length < 2) { michael@0: throw new TypeError('startsWith() requires at least 2 arguments'); michael@0: } michael@0: michael@0: // check if start and end are null/undefined or a 'number' michael@0: if ((start == null) || (isNaN(new Number(start)))) { michael@0: start = 0; michael@0: } michael@0: if ((end == null) || (isNaN(new Number(end)))) { michael@0: end = Number.MAX_VALUE; michael@0: } michael@0: michael@0: // if it's an array michael@0: if (typeof prefix == "object") { michael@0: for (var i = 0, j = prefix.length; i < j; i++) { michael@0: var res = _stringTailMatch(str, prefix[i], start, end, true); michael@0: if (res) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: return _stringTailMatch(str, prefix, start, end, true); michael@0: } michael@0: michael@0: /* michael@0: endsWith(str, suffix[, start[, end]]) -> bool michael@0: michael@0: Return true if str ends with the specified suffix, false otherwise. michael@0: With optional start, test str beginning at that position. michael@0: With optional end, stop comparing str at that position. michael@0: suffix can also be an array of strings to try. michael@0: */ michael@0: function endsWith(str, suffix, start, end) { michael@0: if (arguments.length < 2) { michael@0: throw new TypeError('endsWith() requires at least 2 arguments'); michael@0: } michael@0: michael@0: // check if start and end are null/undefined or a 'number' michael@0: if ((start == null) || (isNaN(new Number(start)))) { michael@0: start = 0; michael@0: } michael@0: if ((end == null) || (isNaN(new Number(end)))) { michael@0: end = Number.MAX_VALUE; michael@0: } michael@0: michael@0: // if it's an array michael@0: if (typeof suffix == "object") { michael@0: for (var i = 0, j = suffix.length; i < j; i++) { michael@0: var res = _stringTailMatch(str, suffix[i], start, end, false); michael@0: if (res) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: return _stringTailMatch(str, suffix, start, end, false); michael@0: } michael@0: michael@0: /* michael@0: Matches the end (direction == false) or start (direction == true) of str michael@0: against substr, using the start and end arguments. Returns false michael@0: if not found and true if found. michael@0: */ michael@0: function _stringTailMatch(str, substr, start, end, fromStart) { michael@0: var len = str.length; michael@0: var slen = substr.length; michael@0: michael@0: var indices = _adjustIndices(start, end, len); michael@0: start = indices[0]; end = indices[1]; len = indices[2]; michael@0: michael@0: if (fromStart) { michael@0: if (start + slen > len) { michael@0: return false; michael@0: } michael@0: } else { michael@0: if (end - start < slen || start > len) { michael@0: return false; michael@0: } michael@0: if (end - slen > start) { michael@0: start = end - slen; michael@0: } michael@0: } michael@0: michael@0: if (end - start >= slen) { michael@0: return str.substr(start, slen) == substr; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: function _adjustIndices(start, end, len) michael@0: { michael@0: if (end > len) { michael@0: end = len; michael@0: } else if (end < 0) { michael@0: end += len; michael@0: } michael@0: michael@0: if (end < 0) { michael@0: end = 0; michael@0: } michael@0: if (start < 0) { michael@0: start += len; michael@0: } michael@0: if (start < 0) { michael@0: start = 0; michael@0: } michael@0: michael@0: return [start, end, len]; michael@0: }