st.ml 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. let camel = "🐫"
  2. let seppo_s = "Seppo.Social"
  3. let seppo_u = Uri.make ~scheme:"https" ~host:seppo_s ()
  4. let seppo_c = "Seppo - Personal Social Web"
  5. (* has an alloc, but part of ocaml >= 4.13 anyway *)
  6. let starts_with ~prefix s =
  7. let lp = prefix |> String.length in
  8. lp <= (s |> String.length)
  9. && (prefix |> String.equal (String.sub s 0 lp))
  10. let after ~prefix s : string option =
  11. let lp = prefix |> String.length in
  12. if lp <= (s |> String.length)
  13. && (prefix |> String.equal (String.sub s 0 lp))
  14. then Some (String.sub s lp ((s |> String.length) - lp))
  15. else None
  16. let before ~suffix s : string option =
  17. let ls = suffix |> String.length
  18. and l = s |> String.length in
  19. if ls <= l
  20. && (suffix |> String.equal (String.sub s (l - ls) ls))
  21. then Some (String.sub s 0 (l - ls))
  22. else None
  23. (* has an alloc, but part of ocaml >= 4.13 anyway *)
  24. let ends_with ~suffix s =
  25. let ls = suffix |> String.length
  26. and l = s |> String.length in
  27. ls <= l
  28. && (suffix |> String.equal (String.sub s (l - ls) ls))
  29. let updir path =
  30. match path
  31. |> String.split_on_char '/'
  32. |> List.fold_left (fun (l,i) s ->
  33. match s,i with
  34. | ("",0) -> (l,0)
  35. | (_,0) -> (l,1)
  36. | (".",i) -> (l,i)
  37. | _ -> (".." :: l,i+1)) ([],0) with
  38. | [],_ -> ""
  39. | l,_ -> (l |> String.concat "/") ^ "/"
  40. let last l =
  41. let flast _ v = v in
  42. List.fold_left flast (List.hd l) l
  43. let is_monotonous cmp l =
  44. let step (r,p) o =
  45. if (not r) || cmp p o < 0
  46. then (false,p)
  47. else (r,o)
  48. in
  49. let r,_ = List.fold_left step (true,List.hd l) l in
  50. r
  51. let window ?(size = 50) ?(page = 0) l =
  52. let mi = page * size in
  53. let ma = mi + size in
  54. l
  55. |> List.filteri (fun i _ -> mi <= i && i < ma)