On 09/13/2017 04:08 AM, Kaitain Jones wrote:
> I have a C++ program that uses SWIPL to obtain terms, some of which it
> will turn into strings. In some cases the terms contain SWI strings
> (this is SWI 7.5).
>
> I had a bug today that was puzzling me for some time: the call
> to PL_get_string_chars(...) would return a NULL string, and report a
> size much larger than I was expecting.
What was the return value of PL_get_string_chars()? If that is not
TRUE, the string and size are not filled.
> It turned out that the problem was (or appeared to be) using the right
> single quote mark ( ’ ) instead of the standard apostrophe ( ' ). I
> imagine somebody cut and pasted the text into the PL code. Anyway, when
> this happens all hell breaks loose. Is this because SWI is expecting a
> matching, closing quote or somesuch? The form of the error was not what
> I would have expected. (Running the PL code itself without the C++
> wrapper exe seems to be okay.)
It is a bit guessing. The ’ is a unicode character (\u2019). If this is
part of the Prolog string, the string will be promoted to a wide
character string and PL_get_string_chars() will fail as this API cannot
deal with Unicode. I consider the PL_get_string_chars() outdated. You
can use PL_get_nchars() or PL_get_wchars(), which allow you to specify
what must be converted, (how) it should be buffered, which encoding must
be used and what to do if the conversion fails. Of course, if you are
sure the target is an ISO-latin-1 string, PL_get_string_chars() is
faster :)
Hope this helps
--- Jan