@tailrec private[this] def appendHelper()

in http-server/src/main/scala/com/twitter/finatra/http/internal/routing/Trie.scala [130:173]


  @tailrec private[this] def appendHelper(node: TrieNode, route: Route, startIndex: Int): Unit = {
    val path = route.path
    val nextIndex = path.indexOf('/', startIndex)
    if (startIndex == 0 || startIndex >= path.length) {
      node.routes.update(route.method.name, route)
      node.pattern += PathPattern(path)
    } else {
      val segment = {
        // when we reached the last character, note we store `/` as part of the segment
        // so we could treat a path with and without trailing slash as 2 different paths
        if (nextIndex == -1 || nextIndex == path.length - 1) path.substring(startIndex, path.length)
        else path.substring(startIndex, nextIndex)
      }
      if (segment == ":*") {
        // update the current node
        node.routes.update(route.method.name, route)
        node.pattern += PathPattern(path)
        // update child node
        node.children.get("*") match {
          case Some(child) =>
            child.routes.update(route.method.name, route)
          case _ =>
            node.children.update(
              "*",
              TrieNode(
                segment = "*",
                constantSegment = false,
                routes = AMap[String, Route](route.method.name -> route),
                pattern = ArrayBuffer[PathPattern](PathPattern(path))
              )
            )
        }
      } else {
        val isConstantSegment = segment == "" || !segment.contains(':')
        val key = if (isConstantSegment) segment else segment.substring(1)

        val child = node.children.getOrElseUpdate(
          key,
          TrieNode(segment = key, constantSegment = isConstantSegment)
        )
        appendHelper(child, route, nextIndex + 1)
      }
    }
  }