// Copyright 2000-2017 JetBrains s.r.o.
//
// 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.intellij.util.text;

import com.intellij.openapi.util.text.CharSequenceWithStringHash;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;

public class CharSequenceSubSequence implements CharSequence, CharArrayExternalizable, CharSequenceWithStringHash {
  private final CharSequence myChars;
  private final int myStart;
  private final int myEnd;

  public CharSequenceSubSequence(@NotNull CharSequence chars) {
    this(chars, 0, chars.length());
  }

  public CharSequenceSubSequence(@NotNull CharSequence chars, int start, int end) {
    if (start < 0 || end > chars.length() || start > end) {
      throw new IndexOutOfBoundsException("chars sequence.length:" + chars.length() +
                                          ", start:" + start +
                                          ", end:" + end);
    }
    myChars = chars;
    myStart = start;
    myEnd = end;
  }

  @Override
  public final int length() {
    return myEnd - myStart;
  }

  @Override
  public final char charAt(int index) {
    return myChars.charAt(index + myStart);
  }

  @NotNull
  @Override
  public CharSequence subSequence(int start, int end) {
    if (start == myStart && end == myEnd) return this;
    return new CharSequenceSubSequence(myChars, myStart + start, myStart + end);
  }

  @NotNull
  public String toString() {
    if (myChars instanceof String) return ((String)myChars).substring(myStart, myEnd);
    return StringFactory.createShared(CharArrayUtil.fromSequence(myChars, myStart, myEnd));
  }

  @NotNull
  CharSequence getBaseSequence() {
    return myChars;
  }

  @Override
  public void getChars(int start, int end, @NotNull char[] dest, int destPos) {
    assert end - start <= myEnd - myStart;
    CharArrayUtil.getChars(myChars, dest, start + myStart, destPos, end - start);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o instanceof CharSequenceSubSequence) return hashCode() == o.hashCode();
    if (o == null || !(o instanceof CharSequence)) return false;
    return StringUtil.equals(this, (CharSequence)o);
  }

  private transient int hash;
  @Override
  public int hashCode() {
    int h = hash;
    if (h == 0) {
      hash = h = StringUtil.stringHashCode(this, 0, length());
    }
    return h;
  }
}

@@ -1,10 +1,13 @@
 /
-/
+*%0A *
  Copyrig
@@ -21,9 +21,9 @@
 -201
-7
+6
  Jet
@@ -36,21 +36,21 @@
  s.r.o.%0A
-//%0A//
+ *%0A *
  License
@@ -106,18 +106,18 @@
 ense%22);%0A
-//
+ *
  you may
@@ -174,18 +174,18 @@
 icense.%0A
-//
+ *
  You may
@@ -217,21 +217,21 @@
 ense at%0A
-//%0A//
+ *%0A *
  http://
@@ -270,13 +270,13 @@
 2.0%0A
-//%0A//
+ *%0A *
  Unl
@@ -340,18 +340,18 @@
 oftware%0A
-//
+ *
  distrib
@@ -409,18 +409,18 @@
  BASIS,%0A
-//
+ *
  WITHOUT
@@ -485,18 +485,18 @@
 mplied.%0A
-//
+ *
  See the
@@ -556,18 +556,18 @@
 ons and%0A
-//
+ *
  limitat
@@ -589,16 +589,20 @@
 License.
+%0A */
 %0A%0Apackag
@@ -2442,292 +2442,8 @@
  %7D%0A%0A
-  @Override%0A  public boolean equals(Object o) %7B%0A    if (this == o) return true;%0A    if (o instanceof CharSequenceSubSequence) return hashCode() == o.hashCode();%0A    if (o == null %7C%7C !(o instanceof CharSequence)) return false;%0A    return StringUtil.equals(this, (CharSequence)o);%0A  %7D%0A%0A
   pr

Revert "PHP php variable, parameter etc. name CharSequence memory usage"