Xyloid's Functions

From Gwen Morse's Wiki
Jump to: navigation, search

Found this here: http://flaprider.dyndns.org/~hair/batmud/scripts.html

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; XYLOID'S FUNCTIONS
;;
;; Adds a new function that overlays a string onto another string at a given position.
;;  1st argument - the original string
;;  2nd argument - the string to overlay
;;  3rd argument - the position to place the 2nd argument.
;;
;; Example: /let s=$[strrep2("this can work", "did", 5)]
;;        s = "this did work"
;;
;; Found here: http://flaprider.dyndns.org/~hair/batmud/scripts.html
;;

/def strrep2 = \
  /let v1=%1%;\
  /let v2=%2%;\
  /let sPos=%3%;\
  /let sLeft=$[substr({v1},0,sPos)]%;\
  /let sRight=$[substr({v1},sPos+strlen({v2}))]%;\
  /return "%{sLeft}%{v2}%{sRight}"

;; "test this out" -> "Test this out"
/def ucfirst = \
  /let v1=%*%;\
  /let v1=$[toupper(v1,1)]%;\
  /return "%{v1}"

;; "test this out" -> "Test This Out"
/def toproper = \
  /let v1=$[ucfirst(%*)]%;\
  /let s1=$[strchr(v1," ")]%;\
  /while (s1 > 0) \
    /let v1=$[strrep2(v1,ucfirst(substr(v1,s1+1)),s1+1)]%;\
    /let s1=$[strchr(v1," ",s1+1)]%;\
  /done%;\
  /return "%{v1}"

/def trim = \
  /let v1=%*%;\
  /if (strlen(v1)<1) /return ""%;/endif%;\
  /let s1=$[strchr(v1," ")]%;\
  /while (s1 == 0) \
    /let v1=$[substr(v1,1)]%;\
    /if (strlen(v1)<1) /return ""%;/endif%;\
    /let s1=$[strchr(v1," ")]%;\
  /done%;\
  /let s1=$[strrchr(v1," ")]%;\
  /while (s1 == (strlen(v1)-1)) \
    /let v1=$[substr(v1,0,strlen(v1)-1)]%;\
    /if (strlen(v1)<1) /return ""%;/endif%;\
    /let s1=$[strrchr(v1," ")]%;\
  /done%;\
  /return "%{v1}"

/def left = \
  /let v1=$[substr(%1,0,%2)]%;\
  /return "%{v1}"

/def right = \
  /let v1=$[substr(%1,-%2)]%;\
  /return "%{v1}"

;; /eval /echo $[chop("ten,")]
;; ten
/def chop = /let v1=$[substr(%*,0,strlen(%*)-1)]%;/return "%{v1}"

/def strcount = \
  /let strpat=%1%;\
  /let strsrch=%2%;\
  /let strcount=0%;\
  /let strpos=$[strstr(strsrch,strpat,0)]%;\
  /while (strpos!=-1) \
    /let strcount=$[strcount+1]%;\
    /let strpos=$[strstr(strsrch,strpat,strpos+strlen(strpat))]%;\
  /done%;\
  /return "%{strcount}"