Created
January 2, 2025 02:54
-
-
Save rendello/49c23aeba3734b9a3e6c443e9a2e493b to your computer and use it in GitHub Desktop.
SQLite extension: Hex str to int conversion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** 2025-01-01 | |
** | |
** The author disclaims copyright to this source code. | |
*/ | |
#include "sqlite3ext.h" | |
SQLITE_EXTENSION_INIT1 | |
#include <stdlib.h> | |
#include <stdio.h> | |
/* | |
** Convert hex string to integer. Allow NULL to pass through, | |
** otherise the value must be a non-empty hex string with an optional | |
** '0x' or '0X' prefix. | |
*/ | |
static void hextoint( | |
sqlite3_context *context, | |
int argc, | |
sqlite3_value **argv | |
){ | |
sqlite_int64 result; | |
int base; | |
const unsigned char *start; | |
char *stop; | |
sqlite3_value *value = argv[0]; | |
int value_type = sqlite3_value_type(value); | |
switch (value_type) { | |
case SQLITE_NULL: | |
sqlite3_result_null(context); | |
return; | |
case SQLITE_TEXT: | |
start = sqlite3_value_text(value); | |
result = strtoll((const char *)start, &stop, 16); | |
if (*start == '\0' || *stop != '\0') | |
break; | |
sqlite3_result_int64(context, result); | |
return; | |
} | |
sqlite3_result_error(context, "expects hex string or NULL", -1); | |
} | |
#ifdef _WIN32 | |
__declspec(dllexport) | |
#endif | |
int sqlite3_hextoint_init( | |
sqlite3 *db, | |
char **pzErrMsg, | |
const sqlite3_api_routines *pApi | |
){ | |
int rc = SQLITE_OK; | |
SQLITE_EXTENSION_INIT2(pApi); | |
(void)pzErrMsg; /* Unused parameter */ | |
sqlite3_create_function(db, "hextoint", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, hextoint, 0, 0); | |
return rc; | |
} |
Yeah, stroll() is borderline for magic number. The curious one is radix 0,
which seems to rely on how the string uses 0x or not.
Thanks for the comment about my original. I'll stick with my native voice,
my original concern was sounding terse or preachy.
ChatGpt has its place, and knowing when to use it is something I'm still
trying to guage.
…On Thu, Jan 23, 2025, 11:09 AM Gaven Rendell ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@aodonnell-ca <https://github.com/aodonnell-ca> I figured, since I looked
up your blog and saw you mention you did something similar there. I figured
most C programmers wouldn't have an h1 heading entitled "pro tips", that's
more of an LLM thing ;) For what it's worth, I think your original tone was
good.
I think I'd disagree about the magic numbers in this case, I feel the fact
that strtoll is well known and takes a radix parameter, coupled with the
fact that my short function is called hextoint and has a documentation
comment means that adding a #define for a RADIX_HEX constant would
detract from the code, in my opinion.
Funnily enough I've also been working with code where strtok seems like
the first choice, but eventually I too decided not to use it. It's
certainly an old-C-style function what with the in place modification.
Thanks again for taking time out of your lunch break to help review my
code, I appreciate it :)
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/rendello/49c23aeba3734b9a3e6c443e9a2e493b#gistcomment-5405628>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AANPTO4KUXII2YM6KNWEHVD2ME46TBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTGNJTGMZDANZWU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Oh, I also document a lot in markdown, so yeah, I'll use headings where I
see fit.
On Thu, Jan 23, 2025, 3:35 PM Alex O'Donnell ***@***.***>
wrote:
… Yeah, stroll() is borderline for magic number. The curious one is radix
0, which seems to rely on how the string uses 0x or not.
Thanks for the comment about my original. I'll stick with my native
voice, my original concern was sounding terse or preachy.
ChatGpt has its place, and knowing when to use it is something I'm still
trying to guage.
On Thu, Jan 23, 2025, 11:09 AM Gaven Rendell ***@***.***>
wrote:
> ***@***.**** commented on this gist.
> ------------------------------
>
> @aodonnell-ca <https://github.com/aodonnell-ca> I figured, since I
> looked up your blog and saw you mention you did something similar there. I
> figured most C programmers wouldn't have an h1 heading entitled "pro tips",
> that's more of an LLM thing ;) For what it's worth, I think your original
> tone was good.
>
> I think I'd disagree about the magic numbers in this case, I feel the
> fact that strtoll is well known and takes a radix parameter, coupled
> with the fact that my short function is called hextoint and has a
> documentation comment means that adding a #define for a RADIX_HEX
> constant would detract from the code, in my opinion.
>
> Funnily enough I've also been working with code where strtok seems like
> the first choice, but eventually I too decided not to use it. It's
> certainly an old-C-style function what with the in place modification.
>
> Thanks again for taking time out of your lunch break to help review my
> code, I appreciate it :)
>
> —
> Reply to this email directly, view it on GitHub
> <https://gist.github.com/rendello/49c23aeba3734b9a3e6c443e9a2e493b#gistcomment-5405628>
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AANPTO4KUXII2YM6KNWEHVD2ME46TBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTGNJTGMZDANZWU52HE2LHM5SXFJTDOJSWC5DF>
> .
> You are receiving this email because you were mentioned.
>
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
> .
>
>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@aodonnell-ca I figured, since I looked up your blog and saw you mention you did something similar there. I figured most C programmers wouldn't have an h1 heading entitled "pro tips", that's more of an LLM thing ;) For what it's worth, I think your original tone was good.
I think I'd disagree about the magic numbers in this case, I feel the fact that
strtoll
takes a radix parameter, coupled with the fact that my short function is calledhextoint
and has a documentation comment means that adding a#define
for aRADIX_HEX
constant would detract from the code, in my opinion.Funnily enough I've also been working with code where
strtok
seems like the first choice, but eventually I too decided not to use it. It's certainly an old-C-style function what with the in place modification.Thanks again for taking time out of your lunch break to help review my code, I appreciate it :)