<commit_before>/**
 * Copyright 2011-2012 eBusiness Information, Groupe Excilys (www.excilys.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.excilys.ebi.gatling.core.util

import java.text.Normalizer
import java.util.concurrent.ConcurrentHashMap
import java.util.regex.Pattern
import scala.annotation.implicitNotFound
import scala.collection.JavaConverters.asScalaConcurrentMapConverter
import scala.collection.mutable.ConcurrentMap
import com.excilys.ebi.gatling.core.session.EvaluatableString
import com.excilys.ebi.gatling.core.session.Session
import grizzled.slf4j.Logging

/**
 * This object groups all utilities for strings
 */
object StringHelper extends Logging {

	val CACHE: ConcurrentMap[String, EvaluatableString] = new ConcurrentHashMap[String, EvaluatableString].asScala

	val END_OF_LINE = System.getProperty("line.separator")

	val EL_START = "${"

	val EL_END = "}"

	val EMPTY = ""

	val INDEX_START = "("

	val INDEX_END = ")"

	val MISSING_SESSION_ATTRIBUTE = "undefined"

	val jdk6Pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+")

	val elPatternString = """\$\{(.+?)\}"""
	val elPattern = elPatternString.r
	val elOccurrencePattern = """(.+?)\((\d+)\)""".r

	/**
	 * Method that strips all accents from a string
	 */
	def stripAccents(string: String) = {
		val normalized = Normalizer.normalize(string, Normalizer.Form.NFD)
		jdk6Pattern.matcher(normalized).replaceAll(EMPTY);
	}

	def parseEvaluatable(stringToFormat: String): EvaluatableString = {

		def parseStaticParts: Array[String] = stringToFormat.split(elPatternString, -1)
		
		def parseDynamicParts: List[Session => Any] = {
			elPattern.findAllIn(stringToFormat).matchData.map { data =>
				val elContent = data.group(1)
				val occurrencePart = elOccurrencePattern.findFirstMatchIn(elContent)

				occurrencePart match {
					case Some(occurrencePartMatch) =>
						val key = occurrencePartMatch.group(1)
						val occurrence = occurrencePartMatch.group(2).toInt
						(session: Session) => session.getAttributeAsOption[Seq[Any]](key) match {
							case Some(x) if (x.size > occurrence) => x(occurrence)
							case _ => {
								error("Couldn't resolve occurrence " + occurrence + " of session multivalued attribute " + key)
								MISSING_SESSION_ATTRIBUTE
							}
						}
					case None =>
						val key = data.group(1)
						(session: Session) => session.getAttributeAsOption[Any](key) match {
							case Some(x) => x
							case None => {
								error("Couldn't resolve session attribute}" + key)
								MISSING_SESSION_ATTRIBUTE
							}
						}
				}
			}.toList
		}

		def doParseEvaluatable: EvaluatableString = {
			val dynamicParts = parseDynamicParts

			if (dynamicParts.isEmpty) {
				// no interpolation
				(session: Session) => stringToFormat

			} else {
				val staticParts = parseStaticParts

				val functions = dynamicParts.zip(staticParts)

				(session: Session) => {<commit_msg>Fix typo<commit_after>/**
 * Copyright 2011-2012 eBusiness Information, Groupe Excilys (www.excilys.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.excilys.ebi.gatling.core.util

import java.text.Normalizer
import java.util.concurrent.ConcurrentHashMap
import java.util.regex.Pattern
import scala.annotation.implicitNotFound
import scala.collection.JavaConverters.asScalaConcurrentMapConverter
import scala.collection.mutable.ConcurrentMap
import com.excilys.ebi.gatling.core.session.EvaluatableString
import com.excilys.ebi.gatling.core.session.Session
import grizzled.slf4j.Logging

/**
 * This object groups all utilities for strings
 */
object StringHelper extends Logging {

	val CACHE: ConcurrentMap[String, EvaluatableString] = new ConcurrentHashMap[String, EvaluatableString].asScala

	val END_OF_LINE = System.getProperty("line.separator")

	val EL_START = "${"

	val EL_END = "}"

	val EMPTY = ""

	val INDEX_START = "("

	val INDEX_END = ")"

	val MISSING_SESSION_ATTRIBUTE = "undefined"

	val jdk6Pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+")

	val elPatternString = """\$\{(.+?)\}"""
	val elPattern = elPatternString.r
	val elOccurrencePattern = """(.+?)\((\d+)\)""".r

	/**
	 * Method that strips all accents from a string
	 */
	def stripAccents(string: String) = {
		val normalized = Normalizer.normalize(string, Normalizer.Form.NFD)
		jdk6Pattern.matcher(normalized).replaceAll(EMPTY);
	}

	def parseEvaluatable(stringToFormat: String): EvaluatableString = {

		def parseStaticParts: Array[String] = stringToFormat.split(elPatternString, -1)

		def parseDynamicParts: List[Session => Any] = {
			elPattern.findAllIn(stringToFormat).matchData.map { data =>
				val elContent = data.group(1)
				val occurrencePart = elOccurrencePattern.findFirstMatchIn(elContent)

				occurrencePart match {
					case Some(occurrencePartMatch) =>
						val key = occurrencePartMatch.group(1)
						val occurrence = occurrencePartMatch.group(2).toInt
						(session: Session) => session.getAttributeAsOption[Seq[Any]](key) match {
							case Some(x) if (x.size > occurrence) => x(occurrence)
							case _ => {
								error("Couldn't resolve occurrence " + occurrence + " of session multivalued attribute " + key)
								MISSING_SESSION_ATTRIBUTE
							}
						}
					case None =>
						val key = data.group(1)
						(session: Session) => session.getAttributeAsOption[Any](key) match {
							case Some(x) => x
							case None => {
								error("Couldn't resolve session attribute " + key)
								MISSING_SESSION_ATTRIBUTE
							}
						}
				}
			}.toList
		}

		def doParseEvaluatable: EvaluatableString = {
			val dynamicParts = parseDynamicParts

			if (dynamicParts.isEmpty) {
				// no interpolation
				(session: Session) => stringToFormat

			} else {
				val staticParts = parseStaticParts

				val functions = dynamicParts.zip(staticParts)

				(session: Session) => {
{'fraction_non_alphanumeric': 0.10615989515072084, 'fraction_numerical': 0.005824959953400321, 'mean_word_length': 3.8743789921930447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '142935', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2050, 'n_words': 525}