From 86ba77f1fd4a0f9e2d57fc01bd1b8ec127436380 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Sat, 28 Feb 2009 20:20:29 +0330 Subject: [PATCH] quran: faster quran_aya --- quran.c | 26 ++++++++++++++++++-------- quran.h | 1 + 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/quran.c b/quran.c index 5f9788d..3673a25 100644 --- a/quran.c +++ b/quran.c @@ -30,24 +30,33 @@ static char *file_read(char *path) return buf; } +static int newlines(char *s) +{ + int n; + for (n = 0; s = strchr(s, '\n'); s++, n++); + return n; +} + struct quran *quran_alloc(char *path) { + int i; + int nayas; struct quran *quran = xmalloc(sizeof(struct quran)); memset(quran, 0, sizeof(quran)); quran->text = file_read(path); + nayas = newlines(quran->text); + quran->ayas = xmalloc(sizeof(quran->ayas) * nayas); + quran->ayas[0] = quran->text; + for (i = 1; i < nayas; i++) + quran->ayas[i] = strchr(quran->ayas[i - 1], '\n') + 1; return quran; } void quran_aya(struct quran *quran, char *buf, size_t len, int aya) { - int i; - char *s = quran->text; - char *e; - int n; - for (i = 0; i < aya; i++) - s = strchr(s, '\n') + 1; - e = strchr(s, '\n'); - n = e - s - 1; + char *s = quran->ayas[aya]; + char *e = strchr(s, '\n'); + size_t n = e - s - 1; if (n > len - 1) n = len - 1; memcpy(buf, s, n); @@ -56,6 +65,7 @@ void quran_aya(struct quran *quran, char *buf, size_t len, int aya) void quran_free(struct quran *quran) { + free(quran->ayas); free(quran->text); free(quran); } diff --git a/quran.h b/quran.h index 356ba08..54a49d7 100644 --- a/quran.h +++ b/quran.h @@ -5,6 +5,7 @@ struct quran { char *text; + char **ayas; }; struct quran *quran_alloc(char *path); -- 2.11.4.GIT