char32_t value from APSInt

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

char32_t value from APSInt

Deep Majumder via cfe-dev
Hi,

Is there a way to get a char32_t value from a variable of type llvm::APSInt?

I tried something like -
const char32_t Ch = Val.getZExtValue();
(where Val is of type llvm::APSInt) but this implicit casting doesn't seem to work as it stores the same value as of type char.

I tried playing around with https://llvm.org/doxygen/ConvertUTF_8h_source.html but couldn't find a way to make this work.

Thanks,

--
Pratyush Das

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: char32_t value from APSInt

Deep Majumder via cfe-dev
On Thu, 11 Feb 2021 at 22:10, Pratyush Das via cfe-dev <[hidden email]> wrote:
Hi,

Is there a way to get a char32_t value from a variable of type llvm::APSInt?

I tried something like -
const char32_t Ch = Val.getZExtValue();
(where Val is of type llvm::APSInt) but this implicit casting doesn't seem to work as it stores the same value as of type char.

I tried playing around with https://llvm.org/doxygen/ConvertUTF_8h_source.html but couldn't find a way to make this work.

Well, an llvm::APSInt represents an arbitrary-width signed or unsigned integer, and a char32_t (generally) represents a UCS-4 code unit / Unicode code point. How do you want to interpret the APSInt, such that it can be converted to a Unicode code point? If the APSInt represents the Unicode code point value, then Val.getLimitedValue() is probably reasonable, but you'll also need to check for Val.isNegative() to avoid the possibility of a narrow negative APSInt getting confused for a small positive integer. (Generally you should also beware of getZExtValue() because it has a precondition that the value fits in 64 bits; it's also questionable to call getZExtValue on an APSInt because it assumes the APSInt represents an unsigned integer.)



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev