Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DISCUSS] Add HTS_PARSE_* parsing flags #260

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,12 @@ long long hts_parse_decimal(const char *str, char **strend, int flags)
}

const char *hts_parse_reg(const char *s, int *beg, int *end)
{
return hts_parse_region(s, NULL, beg, end, HTS_PARSE_THOUSANDS_SEP);
}

const char *
hts_parse_region(const char *s, char **strend, int *beg, int *end, int flags)
{
char *hyphen;
const char *colon = strrchr(s, ':');
Expand All @@ -1877,11 +1883,12 @@ const char *hts_parse_reg(const char *s, int *beg, int *end)
return s + strlen(s);
}

*beg = hts_parse_decimal(colon+1, &hyphen, HTS_PARSE_THOUSANDS_SEP) - 1;
*beg = hts_parse_decimal(colon+1, &hyphen, flags) - 1;
if (*beg < 0) *beg = 0;

// FIXME \0 vs. return NULL
if (*hyphen == '\0') *end = INT_MAX;
else if (*hyphen == '-') *end = hts_parse_decimal(hyphen+1, NULL, HTS_PARSE_THOUSANDS_SEP);
else if (*hyphen == '-') *end = hts_parse_decimal(hyphen+1, strend, flags);
else return NULL;

if (*beg >= *end) return NULL;
Expand Down
21 changes: 15 additions & 6 deletions htslib/hts.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,23 @@ hts_idx_t *hts_idx_load2(const char *fn, const char *fnidx);
*/
long long hts_parse_decimal(const char *str, char **strend, int flags);

/// Equivalent to hts_parse_region(str, NULL, beg, end, HTS_PARSE_THOUSANDS_SEP)
const char *hts_parse_reg(const char *str, int *beg, int *end);

/// Parse a "CHR:START-END"-style region string
/** @param str String to be parsed
@param beg Set on return to the 0-based start of the region
@param end Set on return to the 1-based end of the region
@return Pointer to the colon or '\0' after the reference sequence name,
or NULL if @a str could not be parsed.
/** @param str String to be parsed
@param strend If non-NULL, set on return to point to the first character
in @a str after those forming the parsed region
@param beg Set on return to the 0-based start of the region
@param end Set on return to the 1-based end of the region
@param flags Or'ed-together combination of HTS_PARSE_* flags
@return Pointer to the colon or terminating character after the reference
sequence name, or NULL if @a str could not be parsed.

When @a strend is NULL, a warning will be printed (if hts_verbose is 2
or more) if there are any trailing characters after the region string.
*/
const char *hts_parse_reg(const char *str, int *beg, int *end);
const char *hts_parse_region(const char *str, char **strend, int *beg, int *end, int flags);

hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec);
void hts_itr_destroy(hts_itr_t *iter);
Expand Down