about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-09-04 00:08:32 -0400
committerRich Felker <dalias@aerifal.cx>2011-09-04 00:08:32 -0400
commitc88f36f55623124d09f48631974ca38aaec00057 (patch)
tree3d205e48775df6e9b13f2e765267facf12553bd5
parent32d67e938e8da0f37c59247acee8b10eaf9a113c (diff)
downloadmusl-c88f36f55623124d09f48631974ca38aaec00057.tar.gz
musl-c88f36f55623124d09f48631974ca38aaec00057.tar.xz
musl-c88f36f55623124d09f48631974ca38aaec00057.zip
optimize seek function for memory streams
-rw-r--r--src/stdio/open_memstream.c15
-rw-r--r--src/stdio/open_wmemstream.c15
2 files changed, 6 insertions, 24 deletions
diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c
index 57737098..7fc16204 100644
--- a/src/stdio/open_memstream.c
+++ b/src/stdio/open_memstream.c
@@ -13,21 +13,12 @@ static off_t ms_seek(FILE *f, off_t off, int whence)
 {
 	ssize_t base;
 	struct cookie *c = f->cookie;
-	switch (whence) {
-	case SEEK_SET:
-		base = 0;
-		break;
-	case SEEK_CUR:
-		base = c->pos;
-		break;
-	case SEEK_END:
-		base = c->len;
-		break;
-	default:
-	fail:
+	if (whence>2U) {
+fail:
 		errno = EINVAL;
 		return -1;
 	}
+	base = (size_t [3]){0, c->pos, c->len}[whence];
 	if (off < -base || off > SSIZE_MAX-base) goto fail;
 	return c->pos = base+off;
 }
diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c
index 41b92d21..0db77416 100644
--- a/src/stdio/open_wmemstream.c
+++ b/src/stdio/open_wmemstream.c
@@ -14,21 +14,12 @@ static off_t wms_seek(FILE *f, off_t off, int whence)
 {
 	ssize_t base;
 	struct cookie *c = f->cookie;
-	switch (whence) {
-	case SEEK_SET:
-		base = 0;
-		break;
-	case SEEK_CUR:
-		base = c->pos;
-		break;
-	case SEEK_END:
-		base = c->len;
-		break;
-	default:
-	fail:
+	if (whence>2U) {
+fail:
 		errno = EINVAL;
 		return -1;
 	}
+	base = (size_t [3]){0, c->pos, c->len}[whence];
 	if (off < -base || off > SSIZE_MAX/4-base) goto fail;
 	memset(&c->mbs, 0, sizeof c->mbs);
 	return c->pos = base+off;