Skip to content

Commit 2b83b17

Browse files
committed
Use more idiomatic switch case instead of if else tree
1 parent 8d03288 commit 2b83b17

File tree

1 file changed

+65
-26
lines changed

1 file changed

+65
-26
lines changed

src/isal/isal_shared.h

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,76 @@ static inline int mem_level_to_bufsize(int compression_level, int mem_level,
9797
static void isal_deflate_error(int err)
9898
{
9999
const char * msg = NULL;
100-
if (err == COMP_OK) return;
101-
else if (err == INVALID_FLUSH) msg = "Invalid flush type";
102-
else if (err == INVALID_PARAM) msg = "Invalid parameter";
103-
else if (err == STATELESS_OVERFLOW) msg = "Not enough room in output buffer";
104-
else if (err == ISAL_INVALID_OPERATION) msg = "Invalid operation";
105-
else if (err == ISAL_INVALID_STATE) msg = "Invalid state";
106-
else if (err == ISAL_INVALID_LEVEL) msg = "Invalid compression level.";
107-
else if (err == ISAL_INVALID_LEVEL_BUF) msg = "Level buffer too small.";
108-
else msg = "Unknown Error";
109-
100+
switch (err) {
101+
case COMP_OK: return;
102+
case INVALID_FLUSH:
103+
msg = "Invalid flush type";
104+
break;
105+
case INVALID_PARAM:
106+
msg = "Invalid parameter";
107+
break;
108+
case STATELESS_OVERFLOW:
109+
msg = "Not enough room in output buffer";
110+
break;
111+
case ISAL_INVALID_OPERATION:
112+
msg = "Invalid operation";
113+
break;
114+
case ISAL_INVALID_STATE:
115+
msg = "Invalid state";
116+
break;
117+
case ISAL_INVALID_LEVEL:
118+
msg = "Invalid compression level.";
119+
break;
120+
case ISAL_INVALID_LEVEL_BUF:
121+
msg = "Level buffer too small.";
122+
break;
123+
default:
124+
msg = "Unknown Error";
125+
}
110126
PyErr_Format(IsalError, "Error %d %s", err, msg);
111127
}
112128

113129
static void isal_inflate_error(int err){
114130
const char * msg = NULL;
115-
if (err == ISAL_DECOMP_OK) return;
116-
else if (err == ISAL_END_INPUT) msg = "End of input reached";
117-
else if (err == ISAL_OUT_OVERFLOW) msg = "End of output reached";
118-
else if (err == ISAL_NAME_OVERFLOW) msg = "End of gzip name buffer reached";
119-
else if (err == ISAL_COMMENT_OVERFLOW) msg = "End of gzip comment buffer reached";
120-
else if (err == ISAL_EXTRA_OVERFLOW) msg = "End of extra buffer reached";
121-
else if (err == ISAL_NEED_DICT) msg = "Dictionary needed to continue";
122-
else if (err == ISAL_INVALID_BLOCK) msg = "Invalid deflate block found";
123-
else if (err == ISAL_INVALID_SYMBOL) msg = "Invalid deflate symbol found";
124-
else if (err == ISAL_INVALID_LOOKBACK) msg = "Invalid lookback distance found";
125-
else if (err == ISAL_INVALID_WRAPPER) msg = "Invalid gzip/zlib wrapper found";
126-
else if (err == ISAL_UNSUPPORTED_METHOD) msg = "Gzip/zlib wrapper specifies"
127-
"unsupported compress method";
128-
else if (err == ISAL_INCORRECT_CHECKSUM) msg = "Incorrect checksum found";
129-
else msg = "Unknown error";
130-
131+
switch (err){
132+
case ISAL_DECOMP_OK:
133+
return;
134+
case ISAL_END_INPUT:
135+
msg = "End of input reached";
136+
break;
137+
case ISAL_NAME_OVERFLOW:
138+
msg = "End of gzip name buffer reached";
139+
break;
140+
case ISAL_COMMENT_OVERFLOW:
141+
msg = "End of gzip comment buffer reached";
142+
break;
143+
case ISAL_EXTRA_OVERFLOW:
144+
msg = "End of extra buffer reached";
145+
break;
146+
case ISAL_NEED_DICT:
147+
msg = "Dictionary needed to continue";
148+
break;
149+
case ISAL_INVALID_BLOCK:
150+
msg = "Invalid deflate block found";
151+
break;
152+
case ISAL_INVALID_SYMBOL:
153+
msg = "Invalid deflate symbol found";
154+
break;
155+
case ISAL_INVALID_LOOKBACK:
156+
msg = "Invalid lookback distance found";
157+
break;
158+
case ISAL_INVALID_WRAPPER:
159+
msg = "Invalid gzip/zlib wrapper found";
160+
break;
161+
case ISAL_UNSUPPORTED_METHOD:
162+
msg = "Gzip/zlib wrapper specifies unsupported compress method";
163+
break;
164+
case ISAL_INCORRECT_CHECKSUM:
165+
msg = "Incorrect checksum found";
166+
break;
167+
default:
168+
msg = "Unknown error";
169+
}
131170
PyErr_Format(IsalError, "Error %d %s", err, msg);
132171
}
133172

0 commit comments

Comments
 (0)