Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 12-Sep-2019 21:03:47

ARappy
Member
Registered: 15-Aug-2019
Posts: 12

String manipulation Plugit example

Hi OS3D community,

To my little beginner knowledge, there is no plugit to do usual string manipulation (sorry if i didn't see it and it already exists somewhere), so made one simple example and propose it here, if it could be of any help for someone.

It is just an example, as I made it only with the functions specially needed for my os3D project, but it is not very difficult to modify it, if you want to add some other string functions you need, as there is many more of them in scol. You can find the list here, (mainly in the str... section):

https://svn.scolring.org/trunk/scol/doc … brary.html

This code has certainly to be improved and written more efficiently, as this is my first step in scol langage, but it works.


HOW TO USE:

Jut create a folder "string" in the "misc" plugits folder and create 3 text files to copy the following code in the respective files named:
string.xml, estring.pkg, cstring.pkg and inside this folder "string", create a folder "lang" to put local files: string.french.lang & string.english.lang



The string that you want to manipulate is send to the plugit in the link parameter (for example with a global var: %var%) or send by the previous plugit as usual.


Parameters (inside plugit pannel):


1) Number of occurence: when trying to find a substring in a string, allow to choose which occurence of the substring you want, if there is more than one.
                        If let to 0, when using Action "GetReplace" (see below), all occurences are replaced.

            example:   "i want to go to the zoo" --> with occurence=2 will select the 2nd "to" (the zoo).
         

2) from char: apply string manipulation not from begining, but from the char n (first char is 1).

3) to char: same for the end.

4) Searched string: a substring you want to find in the original string

5) Replacement string: if calling action getReplaced, the searched string (see above) is replaced by this string


ACTION/EVENT:


1) Action: GetPos -> Event: Position    return the position (start at 1) of the first char of the "Searched string" found in orginal string


2) Action: GetSubStr   has 2 usage mode

                       a) if occurence = 0: just return the substring between "from char" to "to char"
                          (like usual Substring, SubStr, MID function etc.)
                       
                       b) if occurence = n:  search occurence n of "Searched string" in the original string and return two events
                       
                          -> Event: PreString   return the part of the string situated before the "Searched string" [all the string if "Searched string" not found]
                          -> Event: SubString   return the part of the string situated after the "Searched string"  [(-1) if "Searched string" not found]
                   
       
        example:   GetSubStr of string "I want to go to the zoo"
                   parameters: occurence=1, Searched string=" go "
                     
                     -->  PreString="I want to"
                          SubString="to the zoo"
                         
                   In my project, "Searched string" is often just a symbol like # that separate two different data that i want to separate at runtime,
                   but need to get them Simultaneously in 2 separate events.
       
       
3) Action: GetReplace  -> Event: Replaced   return the original string, where the "Searched string" is replaced by the "Replacement string" given in parameters
                                            (same as Replace function in javascript)
       
4) Action: LowerCase -->  Event: LowerCase   return lower or uppercase of original string (not sure this comment is really neccessary smile
   Action: UpperCase -->  Event: UpperCase
       
                                           
5) Event: Done    fired when other actions are finished, return just 'nil'
                  (needed when i wanted to know an action is finished to fired the next plugit, but don't need the returned string)
                 

Code:

File: string.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<PLUGIN name="string" version="1.0" type="misc">
  <DESCRIPTION>String Manipulation</DESCRIPTION>
  <HELP></HELP>
  <RESOURCE></RESOURCE>
  <EDITOR>
    <SCRIPT path="./estring.pkg" />
    <PARAM name="occurence" type="value" />
    <PARAM name="start" type="start" />
    <PARAM name="end" type="end" />
    <PARAM name="find" type="find" />
    <PARAM name="replace" type="value" />
  </EDITOR>
  <CLIENT minstance="true">
    <SCRIPT path="./cstring.pkg" />
    <ACTION name="GetPos" />
    <EVENT name="Position" />
    <ACTION name="GetSubString" />
    <EVENT name="PreString" />
    <EVENT name="SubString" />
    <ACTION name="GetReplace" />
    <EVENT name="Replaced" />   
    <EVENT name="Done" />
    <ACTION name="LowerCase" />
    <EVENT name="LowerCase" />
    <ACTION name="UpperCase" />
    <EVENT name="UpperCase" />
  </CLIENT>
</PLUGIN>

File: estring.pkg

fun cbCloseEdit(v)=
   let v -> [ctrlocc ctrlstart ctrlend ctrlfind ctrlrep] in
   let getEdCtrlFloatValue ctrlocc -> nocc in
   let getEdCtrlFloatValue ctrlstart -> start in
   let getEdCtrlFloatValue ctrlend -> end in
   let getEdCtrlEditTextValue ctrlfind -> find in
   let getEdCtrlEditTextValue ctrlrep -> replace in
   ["occurence" ftoa nocc]::
   ["start" ftoa start]::
   ["end" ftoa end]::
   ["find" find]::
   ["replace" replace]::
   nil;;


fun dynamicedit(winstr, inst, viewstr, applybtn)=
  let [400 300] -> [iw ih] in
  (
    setEdWindowSize winstr iw ih;
    
    let atof (getPluginInstanceParam inst "occurence") -> nocc in
    let atof (getPluginInstanceParam inst "start") -> start in
    let atof (getPluginInstanceParam inst "end") -> end in
    
    let (getPluginInstanceParam inst "find") -> find in
    let if find == nil then "" else find -> find in
    let (getPluginInstanceParam inst "replace") -> replace in
    let if replace == nil then "" else replace -> replace in

    let crEdCtrlLabel winstr 10 12 160 20 (loc "OS3STRING_0001") nil -> labeltitle in
    let crEdCtrlFloat winstr 180 10 40 20 nocc 0.0 100.0 1.0 0 nil EDWIN_RESIZE_MW -> ctrlocc in 

    let crEdCtrlLabel winstr 10 37 160 20 (loc "OS3STRING_0002") nil -> labeltitle in
    let crEdCtrlFloat winstr 180 35 40 20 start 1.0 100.0 1.0 0 nil EDWIN_RESIZE_MW -> ctrlstart in 
    
    let crEdCtrlLabel winstr 10 65 160 20 (loc "OS3STRING_0003") nil -> labeltitle in
    let crEdCtrlFloat winstr 180 63 40 20 end 0.0 100.0 1.0 0 nil EDWIN_RESIZE_MW -> ctrlend in            

    let crEdCtrlLabel winstr 10 90 160 20 (loc "OS3STRING_0004") nil -> labeltitle in
    let crEdCtrlEditText winstr 10 108 (iw - 20) 90 find nil EDWIN_RESIZE_MW -> ctrlfind in
    
    let crEdCtrlLabel winstr 10 223 160 20 (loc "OS3STRING_0005") nil -> labeltitle in
    let crEdCtrlEditText winstr 10 241 (iw - 20) 90 replace nil EDWIN_RESIZE_MW -> ctrlrep in
    (
      [mkfun1 @cbCloseEdit [ctrlocc ctrlstart ctrlend ctrlfind ctrlrep] nil];
    );
  );;

  File: cstring.pkg

  fun deleteOb(inst)=
  0;;

  fun GetPos(str, nocc, start, end, find)=
        set nocc = if nocc == 0 then 1 else nocc;      
        let start -> pos in
        let if (end == 0) then (strlen str) else end -> end in
        let 1 -> count in
          if find == nil then (-1) else
           (
              while ( (count <= nocc) && (pos <= end) && (pos != nil) ) do (
                 set pos = (strfind find str pos);
                 if (pos != nil) then set pos = pos + 1;      
                 set count = count + 1;
              );
              let if (pos == nil) then -1 else pos -> pos in
                  pos;
           );;

  fun cbGetPos(inst, from, action, str, reply, p)=
       let p -> [nocc start end find _] in
       let GetPos str nocc start end find -> pos in
           SendPluginEvent inst "Position" itoa pos nil;
           SendPluginEvent inst "Done" nil nil;
  0;;
  
  
  fun cbGetSubString(inst, from, action, str, reply, p)=
       let p -> [nocc start end find _] in
       let start - 1 ->  start in               
       let if ( (end == 0) || (start > end) )then (strlen str) else end -> end in
       let start -> pos in
       let "" -> pre in
       let "" -> sub in
       (
          if nocc != 0 then (      
               set pos = GetPos str nocc start end find;
               if pos != -1 then (
                   set pre = substr str 0 (pos - 1);
                   
                   let pos + (strlen find) - 1 -> start in
                   let end - start -> len in 
                       set sub = substr str start len;
               )
               else (
                   set pre = str;      
                   set sub = "-1";
               )
          )
          else
          (
              set pre = substr str 0 (start - 1);
              let end - start -> len in            
                  set sub = substr str start len;
           );
           
          SendPluginEvent inst "PreString" pre nil;
          SendPluginEvent inst "SubString" sub nil;
          SendPluginEvent inst "Done"      nil nil;
       );
  0;;


 fun GetReplace(str, find, replace, nocc, start, end)=
        let 0 -> pos in 
       let GetPos str nocc start end find -> pos in (
       if ( pos >= 0 ) then (
          let substr str 0 (pos - 1) -> first in
          let substr str (pos - 1 + strlen find) strlen str -> last in ( 
          set str = strcatn first::replace::last::nil;
          str;
          )
       )
       else nil
       )
 ;;

 fun cbGetReplace(inst, from, action, str, reply, p)=
    let p -> [nocc start end find replace] in
    let start - 1 ->  start in              
    let if (end == 0) then (strlen str) else end -> end in
    (  
       if (nocc != 0) then ( 
           let GetReplace str find replace nocc start end -> res in
           if res != nil then (set str = res);
           0                                          
       )                                             
       else (
          let 1 -> loop in  
           while ( loop == 1) do ( 
             let GetReplace str find replace 1 start end -> res in 
             if res != nil then (set str = res; 0;) else (set loop = 0; 0;)
           );
           1                                                
        );                                           
       SendPluginEvent inst "Replaced" str nil;
       SendPluginEvent inst "Done" nil nil;
    );
  0;;

fun cbLowerCase(inst, from, action, str, reply, p)=
    SendPluginEvent inst "LowerCase" (strlowercase str) nil;
0;;

fun cbUpperCase(inst, from, action, str, reply, p)=
    SendPluginEvent inst "UpperCase" (struppercase str) nil;
0;;

fun newOb(inst)=
  let atoi (getPluginInstanceParam inst "occurence") -> nocc in
  let atoi (getPluginInstanceParam inst "start") -> start in
  let atoi (getPluginInstanceParam inst "end") -> end in
  let (getPluginInstanceParam inst "find") -> find in
  let if (find == nil) then "" else find -> find in
  let (getPluginInstanceParam inst "replace") -> replace in
  let if (replace == nil) then "" else replace -> replace in
  let [nocc start end find replace] -> struc in
  (
    PluginRegisterAction inst "GetPos" mkfun6 @cbGetPos struc;
    PluginRegisterAction inst "GetSubString" mkfun6 @cbGetSubString struc;
    PluginRegisterAction inst "GetReplace" mkfun6 @cbGetReplace struc;
    PluginRegisterAction inst "LowerCase" mkfun6 @cbLowerCase struc;
    PluginRegisterAction inst "UpperCase" mkfun6 @cbUpperCase struc;
    setPluginInstanceCbDel inst @deleteOb;
  );
  0;;


fun IniPlug(file)=
  PlugRegister @newOb nil;
  setPluginEditor @dynamicedit;
  0;;

File: string.english.lang (to be put in folder "lang")

LANGUAGE english
OS3STRING_DESC String manipulation: find, substring, replace, case replace
OS3STRING_HELP
OS3STRING_0001 Number of occurence
OS3STRING_0002 from char
OS3STRING_0003 To char
OS3STRING_0004 Searched string
OS3STRING_0005 Replacement string

File: string.french.lang (to be put in folder "lang")

LANGUAGE french
OS3STRING_DESC Manipulation de chaine de caracteres: find, replace, subtring, case
OS3STRING_HELP
OS3STRING_0001 Nombre d'occurences
OS3STRING_0002 A partir du caractère
OS3STRING_0003 Jusqu'au caractére
OS3STRING_0004 Chaîne recherchée
OS3STRING_0005 Chaîne de remplacement

Offline

#2 13-Sep-2019 08:39:12

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,161
Website

Re: String manipulation Plugit example

Great thanks for sharing smile

Offline

Board footer

Powered by FluxBB