Best way to convert whole file to lowercase in C

后端 未结 5 1575
死守一世寂寞
死守一世寂寞 2021-01-21 10:06

I was wondering if theres a realy good (performant) solution how to Convert a whole file to lower Case in C. I use fgetc convert the char to lower case and write it in another t

5条回答
  •  孤街浪徒
    2021-01-21 10:56

    This doesn't really answer the question (community wiki), but here's an (over?)-optimized function to convert text to lowercase:

    #include 
    #include 
    #include 
    
    int fast_lowercase(FILE *in, FILE *out)
    {
        char buffer[65536];
        size_t readlen, wrotelen;
        char *p, *e;
        char conversion_table[256];
        int i;
    
        for (i = 0; i < 256; i++)
            conversion_table[i] = tolower(i);
    
        for (;;) {
            readlen = fread(buffer, 1, sizeof(buffer), in);
            if (readlen == 0) {
                if (ferror(in))
                    return 1;
                assert(feof(in));
                return 0;
            }
    
            for (p = buffer, e = buffer + readlen; p < e; p++)
                *p = conversion_table[(unsigned char) *p];
    
            wrotelen = fwrite(buffer, 1, readlen, out);
            if (wrotelen != readlen)
                return 1;
        }
    }
    

    This isn't Unicode-aware, of course.

    I benchmarked this on an Intel Core 2 T5500 (1.66GHz), using GCC 4.6.0 and i686 (32-bit) Linux. Some interesting observations:

    • It's about 75% as fast when buffer is allocated with malloc rather than on the stack.
    • It's about 65% as fast using a conditional rather than a conversion table.

提交回复
热议问题