def snake

bend-lint-rules@0.2.0.0 · layout/case.bend:106

import bend-lint-rules@0.2.0.0/layout/case.bend as Case
import Base

106# `s` in snake_case: a word starts at a capital after a small letter or digit, or at the last capital of a run before a
107# small letter. `capital`: whether `s` starts with a capital. `prev`: the character before is none or `_` (0), a small
108# letter or digit (1), or a capital (2). `acc`: the result so far, last character first.
109def snake(s: String, capital: Bool, +prev: U32, acc: String) -> String:
110  match s capital:
111    case SNil{} _:
112      String.reverse(acc)
113    case SCon{c, +rest} True{}:
114      snake(
115        rest,
116        has_capital_head(rest),
117        2,
118        snake_cap(
119          Bool.or(U32.is_eq(prev, 1), Bool.and(U32.is_eq(prev, 2), has_small_head(rest))),
120          c,
121          acc
122        )
123      )
124    case SCon{+c, +rest} False{}:
125      snake(rest, has_capital_head(rest), Bool.pick(U32, Char.is_eq(c, '_'), 0, 1), SCon{c, acc})

Uses (13)

Used by (1)