Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String get truncated by one character #1500

Closed
mjmogo opened this issue Jul 8, 2021 · 7 comments
Closed

String get truncated by one character #1500

mjmogo opened this issue Jul 8, 2021 · 7 comments

Comments

@mjmogo
Copy link

mjmogo commented Jul 8, 2021

I've written a UDO to concatenate an array of strings into one string. It also allows for a separator string that is inserted between the strings. If you pass a string with a length of 0 for the separator, rather than doing nothing, it removes one character from the input string. I'm able to get around this by checking for the length of the separator string before concatenating the strings.

This is the output from the test program.

SECTION 1:
[0] 'one'
[1] 'two'
[2] 'three'
'ontwthree' 'one, two, three'
Score finished in csoundPerform().

<Cabbage>
form caption("Untitled") size(400, 300), guiMode("queue"), pluginId("def1")
keyboard bounds(8, 158, 381, 95)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d --midi-key-cps=4 --midi-velocity-amp=5
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
ksmps = 32
nchnls = 2
0dbfs = 1

opcode strarray, S, S[]S
    SArr[], SSep xin
    SStr = ""
    iLen lenarray SArr
    iCount = 0
    while iCount < iLen do
        SStr strcat SStr, SArr[iCount]
        if iCount < (iLen - 1) then
            SStr strcat SStr, SSep
        endif
        iCount = iCount + 1
    od
    xout SStr
endop

instr 1
    SArr[] fillarray "one", "two", "three"
    SVal1 strarray SArr, ""
    SVal2 strarray SArr, ", "
    
    iLen lenarray SArr
    iCount = 0
    while iCount < iLen do
        prints "[%d] '%s'\n", iCount, SArr[iCount]
        iCount = iCount + 1
    od
    prints "'%s' '%s'\n", SVal1, SVal2
endin 


</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
i 1 0 0
;f0 z
</CsScore>
</CsoundSynthesizer>

@vlazzarini
Copy link
Member

ok, maybe not related to UDOs. Could you try making a version without the UDO, putting the code in the instrument and only using an empty string as a separator?

I suspect strcat, because if the problem was with passing the string array to an UDO, then you would see a problem with other terminators.

@mjmogo
Copy link
Author

mjmogo commented Jul 8, 2021

instr 2
  Sout = "one"
  Sout strcat Sout, ""
  puts Sout, 1
endin
schedule(2,0,0)

outputs "on"

Joachim Heintz put this together...

@rorywalsh
Copy link
Contributor

rorywalsh commented Jul 8, 2021 via email

@vlazzarini
Copy link
Member

ok bug in strcat. thx.

@mjmogo
Copy link
Author

mjmogo commented Jul 9, 2021 via email

@jpffitch
Copy link
Contributor

Only happens if the destination string is t he same as a source

instr 3
Sout = "one"
Sout1 strcat Sout, ""
puts Sout1b, 1
endin
schedule(3,1,0)

@jpffitch
Copy link
Contributor

I think I fixed it. in github now

@jpffitch jpffitch closed this as completed Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants