convert primitives (int,float,...) to string (or linear string). Convert native char* to string and reversed ?

67 views
Skip to first unread message

Russoul

unread,
Oct 7, 2017, 5:40:01 AM10/7/17
to ats-lang-users
convert primitives (int,float,...) to string (or linear string). Convert native char* to string and reversed ?

gmhwxi

unread,
Oct 7, 2017, 7:33:13 AM10/7/17
to ats-lang-users

Assuming that you are targeting C. I would suggest that you use snprintf
(plus $UNSAFE.cast).

Russoul

unread,
Oct 7, 2017, 7:56:21 AM10/7/17
to ats-lang-users
And how to correctly convert char* to string ? Just $UNSAFE.cast{string}(pointer) ? Will the original char* be GCed by ATS ?

суббота, 7 октября 2017 г., 14:33:13 UTC+3 пользователь gmhwxi написал:

Hongwei Xi

unread,
Oct 7, 2017, 8:04:09 AM10/7/17
to ats-lan...@googlegroups.com

ATS has no GC.

You can use libgc to do malloc/free needed in the C code generated
from ATS source. In that way, you get GC from libgc.

You can also do $UNSAFE.castvwtp0{Strptr1}(pointer) to get a linear string.
Then you need to explicitly free the linear string at some point in your program
(or cast it into a non-linear string).


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/db92b4d8-58aa-4033-a446-d4f009c0215c%40googlegroups.com.

Artyom Shalkhakov

unread,
Oct 7, 2017, 8:33:23 AM10/7/17
to ats-lang-users
ATS has multiple types for strings:

non linear (string, also length-indexed)
linear (strbuf for C-like construction purposes, strptr and length-indexed strnptr)

If you want to temporarily create a string, and then discard it, you could use strptr.

There is also a contrib package for ref-counted strings. Makes writing code that manipulates strings easier to write.

7 окт. 2017 г. 5:56 PM пользователь "Russoul" <russ...@gmail.com> написал:
--

Russoul

unread,
Oct 7, 2017, 8:48:53 AM10/7/17
to ats-lang-users
Sorry, I'm a bit lost now ) Right, ATS has no GC. But it can generate C code that can be linked with libgc with D_ATS_GCATS, right ? Or I'm still missing something ?
суббота, 7 октября 2017 г., 15:04:09 UTC+3 пользователь gmhwxi написал:

ATS has no GC.

You can use libgc to do malloc/free needed in the C code generated
from ATS source. In that way, you get GC from libgc.

You can also do $UNSAFE.castvwtp0{Strptr1}(pointer) to get a linear string.
Then you need to explicitly free the linear string at some point in your program
(or cast it into a non-linear string).

On Sat, Oct 7, 2017 at 7:56 AM, Russoul <russ...@gmail.com> wrote:
And how to correctly convert char* to string ? Just $UNSAFE.cast{string}(pointer) ? Will the original char* be GCed by ATS ?

суббота, 7 октября 2017 г., 14:33:13 UTC+3 пользователь gmhwxi написал:

Assuming that you are targeting C. I would suggest that you use snprintf
(plus $UNSAFE.cast).

On Saturday, October 7, 2017 at 5:40:01 AM UTC-4, Russoul wrote:
convert primitives (int,float,...) to string (or linear string). Convert native char* to string and reversed ?

--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.

gmhwxi

unread,
Oct 7, 2017, 8:58:08 AM10/7/17
to ats-lang-users

What I was trying to say is that malloc/free is external to ATS;
it is not baked into ATS.

If you use libgc for malloc/free, you get GC from libgc. If you use,
say, libc for malloc/free, then you don't have GC.

Russoul

unread,
Oct 7, 2017, 9:13:23 AM10/7/17
to ats-lang-users
So if I have ATS running with libgc, a char* allocated with libc in embedded into ATS C code and then I cast char* to string: $UN.cast{string}(ptr) . I still have a string that is located in not garbage collected heap, so i have to free it explicitly, correct ? Same with strptr but ATS compiler backs me up and ensures that I free it .

суббота, 7 октября 2017 г., 15:58:08 UTC+3 пользователь gmhwxi написал:
Message has been deleted

gmhwxi

unread,
Oct 7, 2017, 9:29:43 AM10/7/17
to ats-lang-users
That is correct. Mixing libgc with libc is straightfoward, and I did this
a few times.

Mixing libgc with another implementation of GC (e.g., libgcj(?) for Java) is
a big adventure :) I tried but could not do it successfully.

Russoul

unread,
Oct 7, 2017, 9:43:27 AM10/7/17
to ats-lang-users
Thank you for your help
Reply all
Reply to author
Forward
0 new messages