detect(3)
π detect(3) β the manual, but make it make sense π
okay bestie, gather round β this is the detect(3) page from the Library Functions Manual, and we're gonna get you fluent in charset detection like it's nothing. no cap, this one's short but it's got β¨loreβ¨.
β¨ NAME β¨
detect, detect_r β detecting the character set of stuff, and measuring how confident the charset detection is. it's giving detective energy ππ
π¦ SYNOPSIS π¦
#include <chardet.h>
short detect (char * inbuf, DetectObj ** outbuf);
short detect_r (char * inbuf, size_t inlen, DetectObj ** outbuf);
πΈ DESCRIPTION πΈ
So what it does: it stores the charset and the accuracy of inbuf into outbuf. that's the whole vibe. π
π¨ bestie, listen β thedetectAPI is deprecated because this api is not binary safe. use it or replace it with thedetect_rapi. like,detect_ris the main character here,detectis lowkey on its way out. π
π Arguments:
inbufβ the input string for detecting. this is the thing you're snooping on πinlenβ the length of that input string you're detecting. (detect_rsaid "i need the receipts", and honestly, valid.)outbufβ where the information aboutinbufgets stored. the structure ofoutbufgoes like this:
typedef struct DetectObject {
char * encoding;
float confidence;
} DetectObj;
β οΈ heads up: theoutbufvariable must be initialized by thedetect_obj_initAPI before calling this detect api. do not skip this step, bestie, i am begging π
β RETURN VALUE β
it returns the following, case by case, depending on what happened:
CHARDET_SUCCESSβ detecting success π slay, it workedCHARDET_NO_RESULTβ detection failure π it tried and it did not find the vibeCHARDET_NULL_OBJECTβ you didn't initializeoutbufwithchardet_obj_init. bestie. why.CHARDET_OUT_OF_MEMORYβ out of memory happened at an internal API. the memory said "no π "
π EXAMPLE π
here's the whole thing in action, copy-paste ready, iconic behavior only:
#include <chardet.h>
int main (void) {
DetectObj *obj;
char * checkstr = "μλ
νμΈμ";
if ( (obj = detect_obj_init ()) == NULL ) {
fprintf (stderr, "Memory Allocation failed\n");
return CHARDET_MEM_ALLOCATED_FAIL;
}
//switch (detect (checkstr, &obj))
switch (detect_r (checkstr, strlen (checkstr), &obj))
{
case CHARDET_OUT_OF_MEMORY :
fprintf (stderr, "On handle processing, occured out of memory\n");
detect_obj_free (&obj);
return CHARDET_OUT_OF_MEMORY;
case CHARDET_NULL_OBJECT :
fprintf (stderr,
"2st argument of chardet() is must memory allocation "
"with detect_obj_init API\n");
return CHARDET_NULL_OBJECT;
}
# check support obj->bom with CHARDET_BOM_CHECK constant
printf ("encoding: %s, confidence: %f, exists bom: %d\n", obj->encoding, obj->confidence, obj->bom);
detect_obj_free (&obj);
return 0;
}
the way that "μλ
νμΈμ" just sits there being a whole charset detection demo... we love a multilingual moment π«Άβ¨
π AUTHORS π
JoungKyun.Kim <http://oops.org> β the legend themselves. bow down. π
π BUG REPORTS π
found a bug? use the QnA board on https://github.com/Joungkyun/libchardet/issues β that's the spot, don't be shy, they wanna hear it π
π₯ SEE ALSO π₯
detect_handledata(3), detect_obj_init(3), detect_obj_free(3) β go read these too, they're the supporting cast and they ate fr fr π
and that's the page!! this was the libchardet manuals version from 2019-08-01, and it's been a joy. mwah π go detect some encodings, bestie β you've got this. πβ¨π¦
π¦ where this page comes from
- debian trixie: libchardet-dev 1.0.6-1 Β· MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later
- ubuntu questing: libchardet-dev 1.0.6-1
- ubuntu resolute: libchardet-dev 1.0.6-2 Β· MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later
- ubuntu stonking: libchardet-dev 1.0.6-2 Β· MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms