string-replace-stream.js
function buildTable(search) {
  // Based on https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm#Description_of_pseudocode_for_the_table-building_algorithm
  var table = Array(search.length), pos = 2, cnd = 0, searchLen = search.length;
  table[0] = -1;
  table[1] = 0;
  while (pos < searchLen) {
    if (search[pos-1] === search[cnd]) {
      // the substring continues
      cnd++;
      table[pos] = cnd;
      pos++;
    } else if (cnd > 0) {
      // it doesn't, but we can fall back
      cnd = table[cnd];
    } else {
      // we have run part of candidates.  Note cnd = 0
      table[pos] = 0;
      pos++;
    }
  }
  return table;
}
function buildTable(search) {
  // Based on https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm#Description_of_pseudocode_for_the_table-building_algorithm
  var table = Array(search.length), pos = 2, cnd = 0, searchLen = search.length;
  table[0] = -1;
  table[1] = 0;
  while (pos < searchLen) {
    if (search[pos-1] === search[cnd]) {
      // the substring continues
      cnd++;
      table[pos] = cnd;
      pos++;
    } else if (cnd > 0) {
      // it doesn't, but we can fall back
      cnd = table[cnd];
    } else {
      // we have run part of candidates.  Note cnd = 0
      table[pos] = 0;
      pos++;
    }
  }
  return table;
}
function buildTable ( search ) { // Based on https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm#Description_of_pseudocode_for_the_table-building_algorithm var table = Array ( search . length ) , pos = 2 , cnd = 0 , searchLen = search . length ; table [ 0 ] = - 1 ; table [ 1 ] = 0 ; while ( pos < searchLen ) { if ( search [ pos - 1 ] === search [ cnd ] ) { // the substring continues cnd ++ ; table [ pos ] = cnd ; pos ++ ; } else if ( cnd > 0 ) { // it doesn't, but we can fall back cnd = table [ cnd ] ; } else { // we have run part of candidates.  Note cnd = 0 table [ pos ] = 0 ; pos ++ ; } } return table ; }
Build the Knuth-Morris-Pratt table
Build the Knuth - Morris - Pratt table