Thread: Manually reading a character literal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735

    Manually reading a character literal

    So I've started with the easiest to deal with annoyance, escape characters, this is the function I got so far:
    Code:
    int rdEscChr( int_c_voidp_t getchr, void *source, int c, char32_t *c32 ) {
    	uint_least64_t num = 0;
    	char text[5] = {0};
    	str_t str = {0};
    	if ( c !== '\\' ) {
    		c = -1;
    		goto rdEscChr_done;
    	}
    	str.txt = text;
    	str.cap = 5;
    	switch ( (c = getchr(source)) ) {
    	case '0':
    		c = getchr(source);
    		if ( !isdigit(c) )
    			break;
    		c = rdU64_base62( getchr, source, c, 8, 0, &num );
    		goto rdEscChr_done;
    	case 'b': num = '\b'; break;
    	case 'f': num = '\f': break;
    	case 'n': num = '\n'; break;
    	case 'r': num = '\r'; break;
    	case 't': num = '\t'; break;
    	case 'u':
    		text[0] = getchr(source);
    		text[1] = getchr(source);
    		text[2] = getchr(source);
    		text[3] = getchr(source);
    		str.len = 4;
    		(void)rdU64_base62( (int_c_voidp_t)sgetc, &str, sgetc(&str), 16, 0, &num );
    		break;
    	case 'v': num = '\v'; break;
    	case 'x':
    		text[0] = getchr(source);
    		text[1] = getchr(source);
    		str.len = 3;
    		(void)rdU64_base62( (int_c_voidp_t)sgetc, &str, sgetc(&str), 16, 0, &num );
    		break;
    	// Escape characters \ ' " and ^ do not need to be explicitly defined here
    	default: num = c;
    	}
    	c = getchr(source);
    	rdEscChr_done:
    	if ( c32 ) *c32 = (char32_t)num;
    	return c;
    }
    If you want the details of rdU64_base62, str_t and sgetc then refer back to a previous thread of mine: Manually reading an integer literal
    I'll worry about making those C89 compatible later if I decide to put them in a library (which I am considering atm).

    Anyways does anyone see any potential problems or missing stuff?

    Edit: Within a minute or 2 of posting this I noticed a bunch of problems myself and fixed them, I've now replaced the above code with that.
    Last edited by awsdert; 09-05-2019 at 04:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manually reading an integer literal
    By awsdert in forum C Programming
    Replies: 17
    Last Post: 09-04-2019, 03:36 PM
  2. Problems reading in character
    By jjohan in forum C Programming
    Replies: 8
    Last Post: 09-11-2014, 01:45 AM
  3. vsprintf ommit literal percent character
    By Niara in forum C Programming
    Replies: 3
    Last Post: 03-05-2012, 04:34 PM
  4. How to keep reading the same character in a file
    By nndhawan in forum C Programming
    Replies: 5
    Last Post: 04-04-2011, 05:53 PM
  5. reading a character
    By Calavera in forum C Programming
    Replies: 6
    Last Post: 11-23-2004, 12:25 PM

Tags for this Thread