On Tuesday, May 10, 2022 at 8:45:21 PM UTC+2, Nicolas Robert wrote:
> Le lundi 9 mai 2022 à 16:19:22 UTC+2, Nicolas a écrit :
> > Le lundi 9 mai 2022 à 13:32:16 UTC+2, heinrichmartin a écrit :
> > > On Monday, May 9, 2022 at 10:17:34 AM UTC+2, Nicolas Robert wrote:
> > > > I would like to add single quotes, square brackets, newline... to the left and right of my string.
> > > > It seems to me that I have several choices...
> > > > Tcl_ObjPrintf("%s", x) , Tcl_AppendObjToObj(...).
> > > >
> > > > Can you advise me one ? or another ?
> > > StringCatCmd uses Tcl_AppendObjToObj. That should be a reasonable choice.
> > Hi,
> > when I work with strings, I use the Tcl_DString API
> >
> > ++
> Thank you both for your answers.
> I finally chose this function : Tcl_AppendObjToObj(...)
> I have a problem, I have two objects to add to a list one on the left and one on the right.
> Tcl_Obj* newlist = Tcl_NewObj();
> Tcl_AppendObjToObj(newlist, Tcl_NewStringObj("'", 1));
> Tcl_AppendObjToObj(newlist, listObj);
> Tcl_AppendObjToObj(newlist, Tcl_NewStringObj("'", 1));
I am used to the script level, but
* You could init newlist with the left string already or
* reuse the single quote object left and right.
* If I get
https://www.tcl-lang.org/man/tcl8.6/TclLib/StringObj.htm right, then Tcl_AppendObjToObj(Tcl_NewStringObj()) could be simplified to Tcl_AppendToObj().
* I have no experience with refCount, i.e. no idea whether this code is leaking objects. (My guess is that all your Tcl_New*Obj have refCount 0, but they should go through refCount 1, because only _dropping_ to zero would free them ...)
> puts "newlist : [newlist]"
You seem not to share all code. How do you create listObj above?
If newlist is the implementation of a proc, then look into
https://www.tcl-lang.org/man/tcl8.6/TclLib/SetResult.htm.
> newlist : '{a 30 c 40}'
listObj already holds these braces; appending quotes is _not_ the issue.
> How I can remove my curly braces from my 'listObj' ?
> To obtain this result : newlist : 'a 30 c 40'
Besides looking elsewhere in your code (as stated above), maybe you should reconsider why you are mixing C and script level Tcl code. I cannot guess the real-world use case here.