Java StringBuilder.replace Examples

Java StringBuilder.replace - 30 examples found. These are the top rated real world Java examples of StringBuilder.replace extracted from open source projects. You can rate examples to help us improve the quality of examples.
  public void testPsi2DocMergeReplaceAfterAdd() throws Exception {
    StringBuilder buffer = new StringBuilder("0123456789");
    RangeMarker marker = createMarker(buffer.toString(), 2, 5);
    synchronizer.startTransaction(getProject(), document, psiFile);

    synchronizer.insertString(document, 1, "a");
    buffer.insert(1, "a");

    synchronizer.replaceString(document, 3, 4, "a");
    buffer.replace(3, 4, "a");

    synchronizer.replaceString(document, 3, 5, "bb");
    buffer.replace(3, 5, "bb");
    final PsiToDocumentSynchronizer.DocumentChangeTransaction transaction =
        synchronizer.getTransaction(document);
    final Set<Pair<PsiToDocumentSynchronizer.MutableTextRange, StringBuffer>> affectedFragments =
        transaction.getAffectedFragments();
    assertEquals(affectedFragments.size(), 2);

    synchronizer.commitTransaction(document);

    assertEquals(buffer.toString(), document.getText());

    assertValidMarker(marker, 3, 6);
  }
 /**
  * Applies the size filter
  *
  * @param sb message to filter
  * @param front true if apply front sizer, false for back sizer
  * @param player whether or not it is for a player
  */
 private void filterSizer(StringBuilder sb, boolean front, boolean player) {
   Pattern regex = front ? EXPAND_FRONT : EXPAND_BACK;
   Matcher match = regex.matcher(sb);
   int size = sb.length();
   while (match.find()) {
     int playerSize = Integer.parseInt(match.group(2));
     int consoleSize = Integer.parseInt(match.group(3));
     String string = match.group(1);
     if (player) {
       sb.replace(
           match.start() + sb.length() - size,
           match.end(),
           (TextSizer.measureString(string) > playerSize - 2
               ? string
               : TextSizer.expand(string, playerSize, front)));
     } else {
       sb.replace(
           match.start() + sb.length() - size,
           match.end(),
           (string.length() > consoleSize
               ? string
               : TextSizer.expandConsole(string, consoleSize, front)));
     }
   }
 }
Example #3
0
  private String cleanString(String string) {
    string.trim();
    String patt = "'";
    Pattern r = Pattern.compile(patt);

    StringBuilder sb = new StringBuilder(string);
    Matcher m = r.matcher(sb);
    int start = 0;
    String replString = "\\'";
    while (m.find(start)) {

      sb.replace(m.start(), m.end(), "\\'");
      start = m.start() + replString.length();
    }

    patt = "\n";
    r = Pattern.compile(patt);
    m = r.matcher(sb);
    start = 0;
    replString = "\' + char(13) + char(10) + \'";
    while (m.find(start)) {
      sb.replace(m.start(), m.end(), replString);
      start = m.start() + replString.length();
    }
    return sb.toString();
  }
  /** Reads in a tensor */
  public Tensor3D readTensor(String prefix) {
    // prepare a buffer that will contain the configuration value for this
    // tensor component. this code is called a lot of times; to avoid over-
    // loading the garbage collector, we reuse the string buffer that
    // represents the keyname of each component.

    // we'll need to store the entire prefix and then two characters to hold
    // each of the suffices. initialize the first part of the buffer with
    // the prefix itself and the last part of the buffer with dummy suffix
    int start = prefix.length();
    int end = start + 2; // maximum size of part suffix
    StringBuilder key = new StringBuilder(end);
    key.replace(0, start, prefix);

    // read each tensor component into a correspending array slot
    double[] t = tensorValues.get(); // new double[TENSOR_PARTS.length];
    for (int i = 0; i < t.length; ++i) {
      // replace the suffix of the buffer with the one that we are looking
      // for in this iteration
      key.replace(start, end, TENSOR_PARTS[i]);

      // get the value from the configuration; if it isn't found, then
      // use 0.0 as the value for this component
      String s = key.toString();
      t[i] = containsKey(s) ? getDouble(s) : 0.;
    }

    // create the tensor from the array of values
    return new Tensor3D(t[0], t[1], t[2], t[3], t[4], t[5]);
  }
Example #5
0
 /** {@inheritDoc} */
 @Override
 public String toString() {
   StringBuilder result = new StringBuilder(64);
   result.append("[").append(getId()).append(":");
   if (productionLevel != null) {
     result.append(" ").append(productionLevel);
   }
   if (unattended) {
     result.append(" unattended");
   }
   if (!(inputs == null || inputs.isEmpty())) {
     result.append(" [inputs: ");
     for (AbstractGoods input : inputs) {
       result.append(input).append(", ");
     }
     int length = result.length();
     result.replace(length - 2, length, "]");
   }
   if (!(outputs == null || outputs.isEmpty())) {
     result.append(" [outputs: ");
     for (AbstractGoods output : outputs) {
       result.append(output).append(", ");
     }
     int length = result.length();
     result.replace(length - 2, length, "]");
   }
   result.append("]");
   return result.toString();
 }
 private LimitType limitQuery(
     String limitPattern,
     LimitType limitType,
     String queryString,
     StringBuilder queryResult,
     Integer firstResult,
     Integer maxResults) {
   if (limitPattern == null) return null;
   int ix = limitPattern.indexOf("$S");
   if (ix >= 0) {
     limitType.afterSql = limitPattern.indexOf("$", ix + 1) > 0;
     queryResult.append(limitPattern.substring(0, ix));
     queryResult.append(queryString);
     queryResult.append(limitPattern.substring(ix + 2));
   } else {
     ix = limitPattern.indexOf("$s");
     if (ix >= 0) {
       limitType.afterSql = limitPattern.indexOf("$", ix + 1) > 0;
       int ix2 = queryString.toLowerCase().indexOf("select");
       if (ix2 < 0) return null;
       queryResult.append(limitPattern.substring(0, ix));
       queryResult.append(queryString.substring(ix2 + 6));
       queryResult.append(limitPattern.substring(ix + 2));
     } else {
       return null;
     }
   }
   if (limitType.alsoFirst) {
     ix = queryResult.indexOf("$F");
     if (ix >= 0) {
       if (queryResult.indexOf("$m", ix) < 0 && queryResult.indexOf("$M", ix) < 0)
         limitType.maxBeforeFirst = true;
       queryResult.replace(ix, ix + 2, "?");
     } else {
       ix = queryResult.indexOf("$f");
       if (ix >= 0) {
         limitType.zeroBasedFirst = true;
         if (queryResult.indexOf("$m", ix) < 0 && queryResult.indexOf("$M", ix) < 0)
           limitType.maxBeforeFirst = true;
         queryResult.replace(ix, ix + 2, "?");
       } else {
         return null;
       }
     }
   }
   ix = queryResult.indexOf("$M");
   if (ix >= 0) {
     queryResult.replace(ix, ix + 2, "?");
   } else {
     ix = queryResult.indexOf("$m");
     if (ix >= 0) {
       limitType.rowidBasedMax = true;
       queryResult.replace(ix, ix + 2, "?");
     } else {
       return null;
     }
   }
   return limitType;
 }