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

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

There is a newer version: 0.36.0
Show newest version
// Copyright (c) 2020-2023 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.more;

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";
  }

  /**
   * 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 index = 0;
    for (var line : lines) {
      results.append(IntObjTuple2.of(index, line));
      index = index + line.length() + 1;    // 1 for the line separator
    }

    return results.toImmutableSeq();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy