File indexing completed on 2024-05-19 04:00:11

0001 /*
0002     Helper String Functions (copied from cstyle.js to be reused by other indenters)
0003     SPDX-FileCopyrightText: Dominik Haumann <dhdev@gmx.de>
0004     SPDX-FileCopyrightText: Milian Wolff <mail@milianw.de>
0005     SPDX-FileCopyrightText: Gerald Senarclens de Grancy <oss@senarclens.eu>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 
0010 //BEGIN String functions
0011 /**
0012  * \brief Returns \c string without leading spaces.
0013  */
0014 String.prototype.ltrim = function()
0015 {
0016     var i = 0;
0017     for ( ; i < this.length && (this[i] == ' ' || this[i] == '\t'); ++i ) {
0018         // continue
0019     }
0020     return this.substr(i);
0021 }
0022 
0023 /**
0024  * \brief Returns \c string without trailing spaces.
0025  */
0026 String.prototype.rtrim = function()
0027 {
0028     if ( this.length == 0 ) {
0029         return "";
0030     }
0031     var i = this.length - 1;
0032     for ( ; i >= 0 && (this[i] == ' ' || this[i] == '\t'); --i ) {
0033         // continue
0034     }
0035     return this.substr(0, i + 1);
0036 }
0037 
0038 /**
0039  * \brief Returns \c string without leading and trailing spaces.
0040  */
0041 String.prototype.trim = function()
0042 {
0043     return this.rtrim().ltrim();
0044 }
0045 
0046 /**
0047  * \brief Fills with \c size \c char's.
0048  * \return the string itself (for chain calls)
0049  */
0050 String.prototype.fill = function(char, size)
0051 {
0052     var string = "";
0053     for ( var i = 0; i < size; ++i ) {
0054         string += char;
0055     }
0056     return string;
0057 }
0058 
0059 /**
0060  * \brief Check if \c this string ends with a given.
0061  * \returns \c true when string ends with \c needle, \c false otherwise.
0062  */
0063 String.prototype.endsWith = function(needle)
0064 {
0065     return this.substr(- needle.length) == needle;
0066 }
0067 
0068 /**
0069  * \brief Check if \c this string starts with a given.
0070  * \return \c true when string starts with \c needle, \c false otherwise.
0071  */
0072 String.prototype.startsWith = function(needle)
0073 {
0074     return this.substr(0, needle.length) == needle;
0075 }
0076 
0077 /**
0078  * \return \c true when \c needle is contained in \c this string, \c false otherwise.
0079  */
0080 String.prototype.contains = function(needle)
0081 {
0082     return this.indexOf(needle) !== -1;
0083 }
0084 
0085 /**
0086  * \return number of occurrences of \c needle in \c this string
0087  * \c needle can be either a string of regular expression
0088  */
0089 String.prototype.countMatches = function(needle)
0090 {
0091     if (typeof needle === 'string')
0092         return this.split(needle).length - 1;
0093     // the correct type of regular expression in ECMAScript is 'object', but
0094     // qtscript seems to use 'function' instead
0095     else if (typeof needle === 'object' || typeof needle === 'function')
0096         return this.match(needle) ? this.match(RegExp(needle.source, 'g')).length : 0;
0097 }
0098 
0099 //END String functions