<commit_before>/*
 * Copyright 2008 Google Inc.
 *
 * 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.google.template.soy.soytree;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.template.soy.base.SourceLocation;
import com.google.template.soy.base.SourceLocation.Point;
import com.google.template.soy.basetree.CopyState;
import com.google.template.soy.soytree.RawTextNode.SourceOffsets.Reason;
import com.google.template.soy.soytree.SoyNode.StandaloneNode;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

/**
 * Node representing a contiguous raw text section.
 *
 * <p>Important: Do not use outside of Soy code (treat as superpackage-private).
 *
 */
public final class RawTextNode extends AbstractSoyNode implements StandaloneNode {

  /** The special chars we need to re-escape for toSourceString(). */
  private static final Pattern SPECIAL_CHARS_TO_ESCAPE = Pattern.compile("[\n\r\t{}\u00A0]");

  /** Map from special char to be re-escaped to its special char tag (for toSourceString()). */
  private static final ImmutableMap<String, String> SPECIAL_CHAR_TO_TAG =
      ImmutableMap.<String, String>builder()
          .put("\n", "{\\n}")
          .put("\r", "{\\r}")
          .put("\t", "{\\t}")
          .put("{", "{lb}")
          .put("}", "{rb}")
          .put("\u00A0", "{nbsp}")
          .build();

  /** The raw text string (after processing of special chars and literal blocks). */
  private final String rawText;

  @Nullable private final SourceOffsets offsets;

  @Nullable private HtmlContext htmlContext;

  /**
   * @param id The id for this node.
   * @param rawText The raw text string.
   * @param sourceLocation The node's source location.
   */
  public RawTextNode(int id, String rawText, SourceLocation sourceLocation) {
    this(id, rawText, sourceLocation, SourceOffsets.fromLocation(sourceLocation, rawText.length()));
  }

  public RawTextNode(
      int id, String rawText, SourceLocation sourceLocation, HtmlContext htmlContext) {
    super(id, sourceLocation);
    this.rawText = checkNotNull(rawText);
    this.htmlContext = htmlContext;
    this.offsets = SourceOffsets.fromLocation(sourceLocation, rawText.length());
  }

  public RawTextNode(int id, String rawText, SourceLocation sourceLocation, SourceOffsets offsets) {
    super(id, sourceLocation);
    this.rawText = checkNotNull(rawText);
    this.offsets = offsets;
  }

  /**
   * Copy constructor.
   *
   * @param orig The node to copy.
   */
  private RawTextNode(RawTextNode orig, CopyState copyState) {
    super(orig, copyState);
    this.rawText = orig.rawText;
    this.htmlContext = orig.htmlContext;
    this.offsets = orig.offsets;
  }

  /**
   * Gets the HTML source context (typically tag, attribute value, HTML PCDATA, or plain text) which
   * this node emits in. This is used for incremental DOM codegen.
   */
  public HtmlContext getHtmlContext() {
    return Preconditions.checkNotNull(
        htmlContext, "Cannot access HtmlContext before HtmlContextVisitor");
  }

  @Override
  public Kind getKind() {
    return Kind.RAW_TEXT_NODE;
  }

  public void setHtmlContext(HtmlContext value) {
    this.htmlContext = value;
  }

  /** Returns the raw text string (after processing of special chars and literal blocks). */
  public String getRawText() {
    return rawText;
  }

  public boolean isEmpty() {
    return rawText.isEmpty();
  }

  /**
   * Returns true if there was whitespace deleted immediately prior to {@code index}
   *
   * @param index the index in the raw text, this value should be in the range {@code [0,
   *     rawText.length()]} if {@code rawText.length()} is passed, then this is equivalent to asking
   *     if there is trailing whitespace missing.
   * @throws IndexOutOfBoundsException if index is out of range.
   * @return {@code true} if whitespace was dropped
   */
  public boolean missingWhitespaceAt(int index) {
    return offsets == null ? false : offsets.getReasonAt(index) == SourceOffsets.Reason.WHITESPACE;
  }

  public Point locationOf(int i) {
    checkElementIndex(i, rawText.length(), "index");
    if (offsets == null) {
      return Point.UNKNOWN_POINT;
    }
    return offsets.getPoint(rawText, i);
  }

  /** Returns the source location of the given substring. */
  public SourceLocation substringLocation(int start, int end) {
    checkElementIndex(start, rawText.length(), "start");
    checkArgument(start < end);
    checkArgument(end <= rawText.length());
    if (offsets == null) {
      return getSourceLocation();<commit_msg>Add a TODO.<commit_after>/*
 * Copyright 2008 Google Inc.
 *
 * 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.google.template.soy.soytree;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.template.soy.base.SourceLocation;
import com.google.template.soy.base.SourceLocation.Point;
import com.google.template.soy.basetree.CopyState;
import com.google.template.soy.soytree.RawTextNode.SourceOffsets.Reason;
import com.google.template.soy.soytree.SoyNode.StandaloneNode;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

/**
 * Node representing a contiguous raw text section.
 *
 * <p>Important: Do not use outside of Soy code (treat as superpackage-private).
 *
 */
public final class RawTextNode extends AbstractSoyNode implements StandaloneNode {

  /** The special chars we need to re-escape for toSourceString(). */
  private static final Pattern SPECIAL_CHARS_TO_ESCAPE = Pattern.compile("[\n\r\t{}\u00A0]");

  /** Map from special char to be re-escaped to its special char tag (for toSourceString()). */
  private static final ImmutableMap<String, String> SPECIAL_CHAR_TO_TAG =
      ImmutableMap.<String, String>builder()
          .put("\n", "{\\n}")
          .put("\r", "{\\r}")
          .put("\t", "{\\t}")
          .put("{", "{lb}")
          .put("}", "{rb}")
          .put("\u00A0", "{nbsp}")
          .build();

  /** The raw text string (after processing of special chars and literal blocks). */
  private final String rawText;

  @Nullable private final SourceOffsets offsets;

  @Nullable private HtmlContext htmlContext;

  /**
   * @param id The id for this node.
   * @param rawText The raw text string.
   * @param sourceLocation The node's source location.
   */
  public RawTextNode(int id, String rawText, SourceLocation sourceLocation) {
    this(id, rawText, sourceLocation, SourceOffsets.fromLocation(sourceLocation, rawText.length()));
  }

  public RawTextNode(
      int id, String rawText, SourceLocation sourceLocation, HtmlContext htmlContext) {
    super(id, sourceLocation);
    this.rawText = checkNotNull(rawText);
    this.htmlContext = htmlContext;
    this.offsets = SourceOffsets.fromLocation(sourceLocation, rawText.length());
  }

  public RawTextNode(int id, String rawText, SourceLocation sourceLocation, SourceOffsets offsets) {
    super(id, sourceLocation);
    this.rawText = checkNotNull(rawText);
    this.offsets = offsets;
  }

  /**
   * Copy constructor.
   *
   * @param orig The node to copy.
   */
  private RawTextNode(RawTextNode orig, CopyState copyState) {
    super(orig, copyState);
    this.rawText = orig.rawText;
    this.htmlContext = orig.htmlContext;
    this.offsets = orig.offsets;
  }

  /**
   * Gets the HTML source context (typically tag, attribute value, HTML PCDATA, or plain text) which
   * this node emits in. This is used for incremental DOM codegen.
   */
  public HtmlContext getHtmlContext() {
    return Preconditions.checkNotNull(
        htmlContext, "Cannot access HtmlContext before HtmlContextVisitor");
  }

  @Override
  public Kind getKind() {
    return Kind.RAW_TEXT_NODE;
  }

  public void setHtmlContext(HtmlContext value) {
    this.htmlContext = value;
  }

  /** Returns the raw text string (after processing of special chars and literal blocks). */
  public String getRawText() {
    return rawText;
  }

  public boolean isEmpty() {
    return rawText.isEmpty();
  }

  /**
   * Returns true if there was whitespace deleted immediately prior to {@code index}
   *
   * @param index the index in the raw text, this value should be in the range {@code [0,
   *     rawText.length()]} if {@code rawText.length()} is passed, then this is equivalent to asking
   *     if there is trailing whitespace missing.
   * @throws IndexOutOfBoundsException if index is out of range.
   * @return {@code true} if whitespace was dropped
   */
  public boolean missingWhitespaceAt(int index) {
    return offsets == null ? false : offsets.getReasonAt(index) == SourceOffsets.Reason.WHITESPACE;
  }

  public Point locationOf(int i) {
    checkElementIndex(i, rawText.length(), "index");
    if (offsets == null) {
      return Point.UNKNOWN_POINT;
    }
    return offsets.getPoint(rawText, i);
  }

  // TODO(lukes): Move to SourceLocation.
  /** Returns the source location of the given substring. */
  public SourceLocation substringLocation(int start, int end) {
    checkElementIndex(start, rawText.length(), "start");
    checkArgument(start < end);
    checkArgument(end <= rawText.length());
    if (offsets == null) {
      return getSourceLocation();
{'fraction_non_alphanumeric': 0.10741758241758241, 'fraction_numerical': 0.0027472527472527475, 'mean_word_length': 3.6831046312178386, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3399127', 'n_tokens_mistral': 3179, 'n_tokens_neox': 3107, 'n_words': 991}