All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aya.util.StringUtil Maven / Gradle / Ivy

// Copyright (c) 2020-2022 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.util;

import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableList;
import kala.tuple.primitive.IntObjTuple2;
import org.jetbrains.annotations.NotNull;

public interface StringUtil {
  /** Arend */
  static @NotNull String timeToString(long time) {
    if (time < 10000) return time + "ms";
    if (time < 60000) return time / 1000 + ("." + (time / 100 % 10)) + "s";
    var seconds = time / 1000;
    return (seconds / 60) + "m" + (seconds % 60) + "s";
  }

  static @NotNull String trimCRLF(@NotNull String string) {
    return string.replaceAll("\\r\\n?", "\n");
  }

  /**
   * all line separators are treat as 1 character long
   *
   * @return a (index of the first character, line) list
   */
  static @NotNull ImmutableSeq> indexedLines(@NotNull String str) {
    var lines = ImmutableSeq.from(str.lines());
    var results = MutableList.>create();

    var lastLineIndex = -1;
    var lastLineLength = -1;

    for (var line : lines) {
      var index = lastLineIndex == -1
        ? 0
        : lastLineIndex + lastLineLength + 1;

      results.append(IntObjTuple2.of(index, line));
      lastLineIndex = index;
      lastLineLength = line.length();
    }

    return results.toImmutableSeq();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy