Posts: 1,027
Threads: 245
Joined: Jul 2022
10-21-2022, 01:30 PM
(This post was last modified: 10-21-2022, 01:32 PM by Davider.)
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
_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
Posts: 1,335
Threads: 61
Joined: Jul 2006
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
Posts: 1,027
Threads: 245
Joined: Jul 2022
10-21-2022, 10:07 PM
(This post was last modified: 10-21-2022, 10:29 PM by Davider.)
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
_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
Posts: 1,335
Threads: 61
Joined: Jul 2006
10-22-2022, 03:37 AM
(This post was last modified: 10-22-2022, 03:41 AM by Kevin.)
little bit longer code this time
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
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
Posts: 1,027
Threads: 245
Joined: Jul 2022
Add a blank line and the output will change
I think using regular expressions to solve the problem would be more generic.
Macro M1
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
Posts: 1,027
Threads: 245
Joined: Jul 2022
10-23-2022, 12:57 AM
(This post was last modified: 10-23-2022, 12:59 AM by Davider.)
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
_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
Posts: 1,335
Threads: 61
Joined: Jul 2006
that code doesn't work at all. does not move code to the bottom
try this 1
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
Posts: 1,027
Threads: 245
Joined: Jul 2022
10-23-2022, 01:24 AM
(This post was last modified: 10-23-2022, 01:29 AM by Davider.)
The code is a bit difficult to understand,
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
Posts: 1,335
Threads: 61
Joined: Jul 2006
10-23-2022, 01:32 AM
(This post was last modified: 10-23-2022, 01:36 AM by Kevin.)
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.
Posts: 1,027
Threads: 245
Joined: Jul 2022
10-23-2022, 02:04 AM
(This post was last modified: 10-23-2022, 02:25 AM by Davider.)
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
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
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
Posts: 1,335
Threads: 61
Joined: Jul 2006
10-23-2022, 03:01 AM
(This post was last modified: 10-23-2022, 03:13 AM by Kevin.)
change in m1
from
to
or
both mean exactly the same
|