<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import * as React from 'react';

interface ChatTextParserToken {
  expr: RegExp,
  token: number;
}

class ChatTextParser {
  static TEXT: number = 0;
  tokens: ChatTextParserToken[];
  constructor(tokens: ChatTextParserToken[]) {
    this.tokens = tokens;
  }
  public parse(text: string, callback: (token: number, text: string) => JSX.Element[], index: number = 0): JSX.Element[] {
    let html : JSX.Element[] = [];
    let insert : JSX.Element[];
    let section : string;
    let re : RegExp;
    let match: RegExpExecArray;
    let next: number;
    
    if (this.tokens.length > index) {
      re = this.tokens[index].expr;
      next = 0;
      
      // find all matches for this token
      for (match = re.exec(text); match; match = re.exec(text)) {
        // parse text before match
        if (match.index > next) {
          section = text.substr(next, match.index - next);
          insert = this.parse(section, callback, index + 1);
          html = html.concat(insert);                        
        }

        // parse the match
        insert = callback(this.tokens[index].token, match[0]);
        if (!insert) {
          // text didn't match after all, parse again
          insert = this.parse(section, callback, index + 1);
        }
        html = html.concat(insert);                                            

        // track where we are up to                
        next = match.index + match[0].length;
      }
      
      // parse trailing text
      if (next < text.length) {
        section = text.substr(next);
        insert = this.parse(section, callback, index + 1);
        html = html.concat(insert); <commit_msg>Fix issue with handling of unrecognised emoji<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import * as React from 'react';

interface ChatTextParserToken {
  expr: RegExp,
  token: number;
}

class ChatTextParser {
  static TEXT: number = 0;
  tokens: ChatTextParserToken[];
  constructor(tokens: ChatTextParserToken[]) {
    this.tokens = tokens;
  }
  public parse(text: string, callback: (token: number, text: string) => JSX.Element[], index: number = 0): JSX.Element[] {
    let html : JSX.Element[] = [];
    let insert : JSX.Element[];
    let section : string;
    let re : RegExp;
    let match: RegExpExecArray;
    let next: number;
    
    if (this.tokens.length > index) {
      re = this.tokens[index].expr;
      next = 0;
      
      // find all matches for this token
      for (match = re.exec(text); match; match = re.exec(text)) {
        // parse text before match
        if (match.index > next) {
          section = text.substr(next, match.index - next);
          insert = this.parse(section, callback, index + 1);
          html = html.concat(insert);                        
        }

        // parse the match
        insert = callback(this.tokens[index].token, match[0]);
        if (!insert) {
          // text didn't match after all, parse again
          insert = this.parse(match[0], callback, index + 1);
        }
        html = html.concat(insert);                                            

        // track where we are up to                
        next = match.index + match[0].length;
      }
      
      // parse trailing text
      if (next < text.length) {
        section = text.substr(next);
        insert = this.parse(section, callback, index + 1);
        html = html.concat(insert); 
{'fraction_non_alphanumeric': 0.11879106438896189, 'fraction_numerical': 0.006570302233902759, 'mean_word_length': 1.9804228660924041, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5511406', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1073, 'n_words': 350}