time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (1978B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2022 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // VARIABLES //
     22 
     23 var RE = /%(?:([1-9]\d*)\$)?([0 +\-#]*)(\*|\d+)?(?:(\.)(\*|\d+)?)?[hlL]?([%A-Za-z])/g;
     24 
     25 
     26 // FUNCTIONS //
     27 
     28 /**
     29 * Parses a delimiter.
     30 *
     31 * @private
     32 * @param {Array} match - regular expression match
     33 * @returns {Object} delimiter token object
     34 */
     35 function parse( match ) {
     36 	var token = {
     37 		'mapping': ( match[ 1 ] ) ? parseInt( match[ 1 ], 10 ) : void 0,
     38 		'flags': match[ 2 ],
     39 		'width': match[ 3 ],
     40 		'precision': match[ 5 ],
     41 		'specifier': match[ 6 ]
     42 	};
     43 	if ( match[ 4 ] === '.' && match[ 5 ] === void 0 ) {
     44 		token.precision = '1';
     45 	}
     46 	return token;
     47 }
     48 
     49 
     50 // MAIN //
     51 
     52 /**
     53 * Tokenizes a string into an array of string parts and format identifier objects.
     54 *
     55 * @param {string} str - input string
     56 * @returns {Array} tokens
     57 *
     58 * @example
     59 * var tokens = formatTokenize( 'Hello %s!' );
     60 * // returns [ 'Hello ', {...}, '!' ]
     61 */
     62 function formatTokenize( str ) {
     63 	var content;
     64 	var tokens;
     65 	var match;
     66 	var prev;
     67 
     68 	tokens = [];
     69 	prev = 0;
     70 	match = RE.exec( str );
     71 	while ( match ) {
     72 		content = str.slice( prev, RE.lastIndex - match[ 0 ].length );
     73 		if ( content.length ) {
     74 			tokens.push( content );
     75 		}
     76 		tokens.push( parse( match ) );
     77 		prev = RE.lastIndex;
     78 		match = RE.exec( str );
     79 	}
     80 	content = str.slice( prev );
     81 	if ( content.length ) {
     82 		tokens.push( content );
     83 	}
     84 	return tokens;
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = formatTokenize;