Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Move the position of the multiline string
#1
Hi,

I want to Move the position of the multiline string (Red strings) to the bottom
Is there any good way?

Thanks in advance for any advice and help
david

-------------------------------------------------------before:
function Main([string[]]$args)
{
    function D($p1)
    {
        '{D}'
    }
}


function A($p1, $p2)
{
    '{A}'
}

function B($p1)
{
    '{B}'
}

function C()
{
    '{C}'
}


-------------------------------------------------------after:

function A($p1, $p2)
{
    '{A}'
}

function B($p1)
{
    '{B}'
}

function C()
{
    '{C}'
}


function Main([string[]]$args)
{
    function D($p1)
    {
        '{D}'
    }
}


Macro Macro4
Code:
Copy      Help
_s=
;function Main([string[]]$args)
;{
;,function D($p1)
;,{
;,,'{D}'
;,}
;}
;
;function A($p1, $p2)
;{
;,'{A}'
;}
;
;function B($p1)
;{
;,'{B}'
;}
;
;function C()
;{
;,'{C}'
;}

;Todo:  Move the position of the multiline string (function Main) to the bottom
#2
Code:
Copy      Help
str s ss
foreach s _s
,ss.addline(s -1)
,if(!s.len) break
_s.remove(0 ss.len)
ss- "[]"; ss.rtrim
_s.addline(ss -1)
out _s
#3
oh, such a short code, 
It's a bit beyond imagination

Thank you so much

@Kevin

In front, I added two lines of string, How to move?

Macro Macro8
Code:
Copy      Help
_s=
;using namespace Spire.Doc;
;Add-Type -Path .\Spire.Doc.dll
;
;function Main([string[]]$args)
;{
;,function D($p1)
;,{
;,,'{D}'
;,}
;}
;
;function A($p1, $p2)
;{
;,'{A}'
;}
;
;function B($p1)
;{
;,'{B}'
;}
;
;function C()
;{
;,'{C}'
;}

;Todo:  Move the position of the multiline string (function Main)
str s ss
foreach s _s
,ss.addline(s -1)
,if(!s.len) break
_s.remove(0 ss.len)
ss- "[]"; ss.rtrim
_s.addline(ss -1)
out _s
#4
little bit longer code this time

Code:
Copy      Help
str s ss
int i ii lines
foreach s _s
,if(ii=2) break
,if(!s.len)
,,ii+1
,if(ii=1)
,,ss.addline(s -1)
,,lines+1
,i+1
_s.RemoveLineN(i-lines-1 lines)
ss- "[]"; ss.rtrim
_s.addline(ss -1)
out _s

 also will need this member function

Member function str.RemoveLineN
Code:
Copy      Help
function# lineindex [nlines]

;Removes specified line(s).
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.

;lineindex - zero-based line index.
;nlines - number of lines to remove. Default or 0: 1 line.

;EXAMPLE
;str s="zero[]one[]two"
;s.RemoveLineN(1)
;out s


if(nlines<1) nlines=1
int i=findl(this lineindex)
if(i>=0) this.remove(i findl(this+i nlines))
ret i
#5
Add a blank line and the output will change

I think using regular expressions to solve the problem would be more generic. 


Macro M1
Code:
Copy      Help
out
_s=
;using namespace Spire.Doc;
;
;using namespace Spire.Doc.Documents;
;
;function Main([string[]]$args)
;{
;,function D($p1)
;,{
;,,'{D}'
;,}
;}
;
;function A($p1, $p2)
;{
;,'{A}'
;}
;
;function B($p1)
;{
;,'{B}'
;}
;
;function C()
;{
;,'{C}'
;}

;Todo:  Move the position of the multiline string (function Main)
str s ss
int i ii lines
foreach s _s
,if(ii=2) break
,if(!s.len)
,,ii+1
,if(ii=1)
,,ss.addline(s -1)
,,lines+1
,i+1
_s.RemoveLineN(i-lines-1 lines)
ss- "[]"; ss.rtrim
_s.addline(ss -1)
out _s
#6
If there are empty lines between functions, it also affects the result

Only move the main function body to the bottom, and do not change the other line content

Macro M2
Code:
Copy      Help
_s=
;function Main([string[]]$args)
;{
;,function D($p1)
;,{
;,,'{D}'
;,}
;}
;function A($p1, $p2)
;{
;,'{A}'
;}
;
;function B($p1)
;{
;,'{B}'
;}
;function C()
;{
;,'{C}'
;}

;Todo:  Move the position of the multiline string (function Main)
str s1 s2
foreach s1 _s
,s2.addline(s1 -1)
,if(!s1.len) break
_s.remove(0 s2.len)
s2- "[]"; s2.rtrim
_s.addline(s2 -1)
out _s
#7
that code doesn't work at all. does not move code to the bottom

try this 1
 
Code:
Copy      Help
str ss s; int iii i ii lines ec sline
foreach s _s
,if(s.beg("function Main")) 
,,sline=i
,,ec=1
,,ii+1
,if(ii=1)
,,if(ec =0) break
,,ss.addline(s -1)
,,lines+1        
,,if(findc(s '{')<>-1)
,,,iii+1
,,if(findc(s '}')<>-1)
,,,iii-1
,,,if iii=0
,,,,ec=0
,i+1
_s.RemoveLineN(sline lines)
ss- "[]"; ss.rtrim
_s.addline(ss -1)
_s.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[][]");;replace multiple blank lines to 1
out _s
#8
The code is a bit difficult to understand,  Smile
The following string is always unchanged, fixed, 
function Main(
...
function


My thoughts are:
Use regular matching to the Main function body, save to the variable b, delete it from _s, and append var b to the after of the _s
#9
Just because you don't understand the code, doesn't mean it doesn't work. Not everything can be solved using regex. In this case the problem is the curly brackets. Main function could have 1 set or 20 sets of curly brackets
this code solves that problem.
 this code finds function main removes it from string and adds it back to the bottom.
#10
You are right 

Works well, 

The code can work in almost any situation, thanks again

I want to define a subfunction that does not return a value from the result

macro M2 is successful, macro M1 is not successful, how to fix it?

Macro M2
 
Code:
Copy      Help
out
_s=
;using namespace Spire.Doc;
;using namespace Spire.Doc.Documents;
;
;function Main([string[]]$args)
;{
;,function D($p1)
;,{
;,,'{D}'
;,}
;}
;function A($p1, $p2)
;{
;,,'{A}'
;}
;
;function B($p1)
;{
;,,'{B}'
;}
;function C()
;{
;,,'{C}'
;}

out sub.mvmain(_s)

#sub mvmain
function'str str's2
_s=s2
str ss s; int iii i ii lines ec sline
foreach s _s
,if(s.beg("function Main"))
,,sline=i
,,ec=1
,,ii+1
,if(ii=1)
,,if(ec =0) break
,,ss.addline(s -1)
,,lines+1
,,if(findc(s '{')<>-1)
,,,iii+1
,,if(findc(s '}')<>-1)
,,,iii-1
,,,if iii=0
,,,,ec=0
,i+1
_s.RemoveLineN(sline lines)
ss- "[]"; ss.rtrim
_s.addline(ss -1)
_s.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[][]");;replace multiple blank lines to 1
ret _s
;out _s


Macro M1
Code:
Copy      Help
out
_s=
;using namespace Spire.Doc;
;using namespace Spire.Doc.Documents;
;
;function Main([string[]]$args)
;{
;,function D($p1)
;,{
;,,'{D}'
;,}
;}
;function A($p1, $p2)
;{
;,,'{A}'
;}
;
;function B($p1)
;{
;,,'{B}'
;}
;function C()
;{
;,,'{C}'
;}

sub.mvmain(_s)

out _s

#sub mvmain
function &s2
_s=s2
str ss s; int iii i ii lines ec sline
foreach s _s
,if(s.beg("function Main"))
,,sline=i
,,ec=1
,,ii+1
,if(ii=1)
,,if(ec =0) break
,,ss.addline(s -1)
,,lines+1
,,if(findc(s '{')<>-1)
,,,iii+1
,,if(findc(s '}')<>-1)
,,,iii-1
,,,if iii=0
,,,,ec=0
,i+1
_s.RemoveLineN(sline lines)
ss- "[]"; ss.rtrim
_s.addline(ss -1)
_s.replacerx("(?:(?:(?!\n)\s)*\n){2,}" "[][]");;replace multiple blank lines to 1
s2=_s
;out _s
#11
change in m1
from
 
Code:
Copy      Help
function &s2
to
 
Code:
Copy      Help
function ~&s2
or 
 
Code:
Copy      Help
function str&s2

both mean exactly the same


Forum Jump:


Users browsing this thread: 1 Guest(s)