WEBVTT_INTERN webvtt_status webvtt_class_state( webvtt_byte **position, webvtt_token_state *token_state, webvtt_stringlist *css_classes ) { webvtt_string buffer; webvtt_status status = WEBVTT_SUCCESS; CHECK_MEMORY_OP( webvtt_create_string( 1, &buffer ) ); for( ; *token_state == START_TAG_CLASS; (*position)++ ) { if( **position == UTF8_TAB || **position == UTF8_FORM_FEED || **position == UTF8_SPACE || **position == UTF8_LINE_FEED || **position == UTF8_CARRIAGE_RETURN) { CHECK_MEMORY_OP_JUMP( status, webvtt_stringlist_push( css_classes, &buffer ) ); *token_state = START_TAG_ANNOTATION; return WEBVTT_SUCCESS; } else if( **position == UTF8_GREATER_THAN || **position == UTF8_NULL_BYTE ) { CHECK_MEMORY_OP_JUMP( status, webvtt_stringlist_push( css_classes, &buffer ) ); webvtt_release_string( &buffer ); return WEBVTT_SUCCESS; } else if( **position == UTF8_FULL_STOP ) { CHECK_MEMORY_OP_JUMP( status, webvtt_stringlist_push( css_classes, &buffer ) ); webvtt_release_string( &buffer ); CHECK_MEMORY_OP( webvtt_create_string( 1, &buffer ) ); } else { CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( &buffer, **position ) ); } } dealloc: webvtt_release_string( &buffer ); return status; }
WEBVTT_INTERN webvtt_status webvtt_class_state( webvtt_byte **position, webvtt_token_state *token_state, webvtt_stringlist *css_classes ) { webvtt_string buffer; webvtt_status status = WEBVTT_SUCCESS; CHECK_MEMORY_OP( webvtt_create_string( 1, &buffer ) ); for( ; *token_state == START_TAG_CLASS; (*position)++ ) { if( **position == '\t' || **position == '\f' || **position == ' ' || **position == '\n' || **position == '\r') { CHECK_MEMORY_OP_JUMP( status, webvtt_stringlist_push( css_classes, &buffer ) ); *token_state = START_TAG_ANNOTATION; return WEBVTT_SUCCESS; } else if( **position == '>' || **position == '\0' ) { CHECK_MEMORY_OP_JUMP( status, webvtt_stringlist_push( css_classes, &buffer ) ); webvtt_release_string( &buffer ); return WEBVTT_SUCCESS; } else if( **position == '.' ) { CHECK_MEMORY_OP_JUMP( status, webvtt_stringlist_push( css_classes, &buffer ) ); webvtt_release_string( &buffer ); CHECK_MEMORY_OP( webvtt_create_string( 1, &buffer ) ); } else { CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( &buffer, **position ) ); } } dealloc: webvtt_release_string( &buffer ); return status; }
WEBVTT_INTERN webvtt_status webvtt_escape_state( webvtt_byte **position, webvtt_token_state *token_state, webvtt_string *result ) { webvtt_string buffer; webvtt_status status = WEBVTT_SUCCESS; CHECK_MEMORY_OP_JUMP( status, webvtt_create_string( 1, &buffer ) ); /** * Append ampersand here because the algorithm is not able to add it to the * buffer when it reads it in the DATA state tokenizer. */ CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( &buffer, UTF8_AMPERSAND ) ); for( ; *token_state == ESCAPE; (*position)++ ) { /** * We have encountered a token termination point. * Append buffer to result and return success. */ if( **position == UTF8_NULL_BYTE || **position == UTF8_LESS_THAN ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_append_string( result, &buffer ) ); goto dealloc; } /** * This means we have enocuntered a malformed escape character sequence. * This means that we need to add that malformed text to the result and * recreate the buffer to prepare for a new escape sequence. */ else if( **position == UTF8_AMPERSAND ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_append_string( result, &buffer ) ); webvtt_release_string( &buffer ); CHECK_MEMORY_OP_JUMP( status, webvtt_create_string( 1, &buffer ) ); CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( &buffer, *position[0] ) ); } /** * We've encountered the semicolon which is the end of an escape sequence. * Check if buffer contains a valid escape sequence and if it does append * the interpretation to result and change the state to DATA. */ else if( **position == UTF8_SEMI_COLON ) { if( webvtt_string_is_equal( &buffer, "&", 4 ) ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( result, '&' ) ); } else if( webvtt_string_is_equal( &buffer, "<", 3 ) ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( result, '<' ) ); } else if( webvtt_string_is_equal( &buffer, ">", 3 ) ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( result, '>' ) ); } else if( webvtt_string_is_equal( &buffer, "&rlm", 4 ) ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_append( result, rlm_replace, RLM_REPLACE_LENGTH ) ); } else if( webvtt_string_is_equal( &buffer, "&lrm", 4 ) ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_append( result, lrm_replace, LRM_REPLACE_LENGTH ) ); } else if( webvtt_string_is_equal( &buffer, " ", 5 ) ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_append( result, nbsp_replace, NBSP_REPLACE_LENGTH ) ); } else { CHECK_MEMORY_OP_JUMP( status, webvtt_string_append_string( result, &buffer ) ); CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( result, **position ) ); } *token_state = DATA; status = WEBVTT_UNFINISHED; } /** * Character is alphanumeric. This means we are in the body of the escape * sequence. */ else if( webvtt_isalphanum( **position ) ) { CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( &buffer, **position ) ); } /** * If we have not found an alphanumeric character then we have encountered * a malformed escape sequence. Add buffer to result and continue to parse * in DATA state. */ else { CHECK_MEMORY_OP_JUMP( status, webvtt_string_append_string( result, &buffer ) ); CHECK_MEMORY_OP_JUMP( status, webvtt_string_putc( result, **position ) ); status = WEBVTT_UNFINISHED; *token_state = DATA; } } dealloc: webvtt_release_string( &buffer ); return status; }