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

translit.Language.scala Maven / Gradle / Ivy

The newest version!
package translit

trait Language {
  /**
    * Convert Latin character `c` to Cyrillic
    *
    * @param latin     Latin input, excluding next character
    * @param cyrillic  Mapped Cyrillic letters
    * @param append    Latin letter to be mapped next
    *
    * @return (-n, r)  Replace last `n` characters by `r` with `left.length >= n`
    *         ( 0, r)  Append string `r`
    */
  def latinToCyrillicIncremental(
    latin: String, cyrillic: String, append: Char
  ): (Int, String)

  def cyrillicToLatinIncremental(cyrillic: String, letter: Char): (Int, String)

  def latinToCyrillic(text: String): String = {
    val result = new StringBuilder(text.length)
    var offset = 0

    while (offset < text.length) {
      val (length, c) = latinToCyrillicIncremental(
        text.take(offset), result.mkString, text(offset))
      if (length < 0) result.setLength(result.length + length)
      result.append(c)
      offset += 1
    }

    result.mkString
  }

  def cyrillicToLatin(text: String): String = {
    val result = new StringBuilder(text.length * 2)
    var offset = 0

    while (offset < text.length) {
      val (length, c) = cyrillicToLatinIncremental(
        text.take(offset), text(offset))
      if (length < 0) result.setLength(result.length + length)
      result.append(c)
      offset += 1
    }

    result.mkString
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy