mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-10 13:29:58 -05:00
Add pa_sample_format_valid()
I think this makes the code a bit nicer to read and write. This also reduces the chances of off-by-one errors when checking the bounds of the sample format value.
This commit is contained in:
parent
643eb5bae2
commit
1cda717252
8 changed files with 30 additions and 45 deletions
|
|
@ -52,8 +52,7 @@ static const size_t size_table[] = {
|
|||
};
|
||||
|
||||
size_t pa_sample_size_of_format(pa_sample_format_t f) {
|
||||
pa_assert(f >= 0);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
return size_table[f];
|
||||
}
|
||||
|
|
@ -104,6 +103,10 @@ pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec) {
|
|||
return spec;
|
||||
}
|
||||
|
||||
int pa_sample_format_valid(unsigned format) {
|
||||
return format < PA_SAMPLE_MAX;
|
||||
}
|
||||
|
||||
int pa_sample_spec_valid(const pa_sample_spec *spec) {
|
||||
pa_assert(spec);
|
||||
|
||||
|
|
@ -111,8 +114,7 @@ int pa_sample_spec_valid(const pa_sample_spec *spec) {
|
|||
spec->rate > PA_RATE_MAX ||
|
||||
spec->channels <= 0 ||
|
||||
spec->channels > PA_CHANNELS_MAX ||
|
||||
spec->format >= PA_SAMPLE_MAX ||
|
||||
spec->format < 0))
|
||||
!pa_sample_format_valid(spec->format)))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
|
|
@ -152,7 +154,7 @@ const char *pa_sample_format_to_string(pa_sample_format_t f) {
|
|||
[PA_SAMPLE_S24_32BE] = "s24-32be",
|
||||
};
|
||||
|
||||
if (f < 0 || f >= PA_SAMPLE_MAX)
|
||||
if (!pa_sample_format_valid(f))
|
||||
return NULL;
|
||||
|
||||
return table[f];
|
||||
|
|
@ -245,8 +247,7 @@ pa_sample_format_t pa_parse_sample_format(const char *format) {
|
|||
}
|
||||
|
||||
int pa_sample_format_is_le(pa_sample_format_t f) {
|
||||
pa_assert(f >= PA_SAMPLE_U8);
|
||||
pa_assert(f < PA_SAMPLE_MAX);
|
||||
pa_assert(pa_sample_format_valid(f));
|
||||
|
||||
switch (f) {
|
||||
case PA_SAMPLE_S16LE:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue