package Tags;

use strict;

#######
## PARSE TAGS
#######
sub parse()
{
	my $tag_str = shift;
	
	my @tags;
	
	my $curr_tag = '';
	my $quote_open = 0;
	for (my $i = 0; $i < length($tag_str); $i++) {
		my $char = substr($tag_str, $i, 1);
		if ($char eq '"' && $quote_open == 0) { # found an opening quote
			$quote_open = 1;
			if ($curr_tag) {
				push @tags, $curr_tag;
				$curr_tag = '';
			}
		} elsif ($char eq '"' && $quote_open == 1) { # found a closing quote
			$quote_open = 0;
			if (length($curr_tag) > 0) {
				push @tags, $curr_tag;
			}
			$curr_tag = '';
		} elsif (($char =~ /\s/ || $char eq ',') && $quote_open == 0) { # found whitespace without a quoted tag
			if (length($curr_tag) > 0) {
				push @tags, $curr_tag;
			}
			$curr_tag = '';
		} else { # we either have an alphanumeric character or symbol, or whitespace in a quoted tag
			$curr_tag .= $char;
		}
	}

	# add anything left over
	if (length($curr_tag) > 0) {
		push @tags, $curr_tag;
	}

	return @tags;
}


1;
