David Oswald
2012-05-07 16:50:18 UTC
I'm trying to figure out a sane approach to string comparisons in
extension code. I'm using Inline::C on this one. The simplest
solution turns out to be the hardest: Unpack an SV into a C-String,
and use C's comparison functions. Why is that the hardest? Because
it ignores Unicode. However, Perl's 'cmp', 'le', 'ge', 'lt', 'gt',
and 'eq' know about Unicode. So I wanted to compare the two SV's
using Perl's native operations. I see some documentation in perlapi,
but it's so terse I could benefit from an example.
So... does anyone know how these would be implemented in C as XS
extension code such that the comparisons invoke Perl's native tools?
sub compare {
return $_[0] cmp $_[1];
}
.... A C stub:
int compare( SV* left, SV* right ) {
int result;
// Compare the two SV's stringwise.
return result;
}
....and....
sub lessthan {
return $_[0] lt $_[1];
}
A C stub:
int lessthan( SV* left, SV* right ) {
int result;
// Less-than compare the two SV's stringwise.
return result;
}
My goal is to use Inline::C and Inline::C2XS to produce an XS version
of my List::BinarySearch module. It would be named
List::BinarySearch::XS, and I would convert List::BinarySearch to
auto-detect the XS version's presence, substituting the XS versions in
place of the Pure Perl versions if the XS module exists. Not
surprisingly, the documentation on dealing with strings in extension
code is a bit opaque.
Dave
extension code. I'm using Inline::C on this one. The simplest
solution turns out to be the hardest: Unpack an SV into a C-String,
and use C's comparison functions. Why is that the hardest? Because
it ignores Unicode. However, Perl's 'cmp', 'le', 'ge', 'lt', 'gt',
and 'eq' know about Unicode. So I wanted to compare the two SV's
using Perl's native operations. I see some documentation in perlapi,
but it's so terse I could benefit from an example.
So... does anyone know how these would be implemented in C as XS
extension code such that the comparisons invoke Perl's native tools?
sub compare {
return $_[0] cmp $_[1];
}
.... A C stub:
int compare( SV* left, SV* right ) {
int result;
// Compare the two SV's stringwise.
return result;
}
....and....
sub lessthan {
return $_[0] lt $_[1];
}
A C stub:
int lessthan( SV* left, SV* right ) {
int result;
// Less-than compare the two SV's stringwise.
return result;
}
My goal is to use Inline::C and Inline::C2XS to produce an XS version
of my List::BinarySearch module. It would be named
List::BinarySearch::XS, and I would convert List::BinarySearch to
auto-detect the XS version's presence, substituting the XS versions in
place of the Pure Perl versions if the XS module exists. Not
surprisingly, the documentation on dealing with strings in extension
code is a bit opaque.
Dave
--
David Oswald
daoswald-***@public.gmane.org
David Oswald
daoswald-***@public.gmane.org