From b0cd85bd211658a34bf4b59d60efcb685a9a59ab Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Fri, 16 Apr 2021 10:03:08 +0200 Subject: [PATCH] velthuis.c - make bidirectional --- tests/velthuis.c | 40 +++++++++++++++++++++++++++++----------- velthuis.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- velthuis.h | 3 ++- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/tests/velthuis.c b/tests/velthuis.c index 138afcf..30e7b20 100644 --- a/tests/velthuis.c +++ b/tests/velthuis.c @@ -2,27 +2,45 @@ #include "velthuis.h" #include "../velthuis.h" -static void test_encoding(const char *in, const char *expected) +static void test_encoding_to_iast(const char *in, const char *expected) { - char *iast; - encode_velthuis_to_iast_punctation(in, &iast); - ck_assert_str_eq(expected, iast); - free(iast); + char *out; + + encode_velthuis_to_iast(in, &out); + ck_assert_str_eq(expected, out); + free(out); } -START_TEST(test_encode_punctation) +static void test_encoding_to_velthuis(const char *in, const char *expected) { - test_encoding("sa.msk.rtam", "saṃskṛtam"); + char *out; - test_encoding("yoga.h", "yogaḥ"); + encode_iast_to_velthuis(in, &out); + ck_assert_str_eq(expected, out); + free(out); +} - test_encoding("tantra\"saastram", "tantraśāstram"); +START_TEST(test_encode_velthuis_to_iast) +{ + test_encoding_to_iast("sa.msk.rtam", "saṃskṛtam"); + test_encoding_to_iast("yoga.h", "yogaḥ"); + test_encoding_to_iast("tantra\"saastram", "tantraśāstram"); + test_encoding_to_iast("Aa AA - II Ii - .RR .Rr", "Ā Ā - Ī Ī - Ṝ Ṝ"); + test_encoding_to_iast("atha prathamo.adhyaaya.h", "atha prathamo'dhyāyaḥ"); +} +END_TEST - test_encoding("Aa AA - II Ii - .RR .Rr", "Ā Ā - Ī Ī - Ṝ Ṝ"); +START_TEST(test_encode_iast_to_velthuis) +{ + test_encoding_to_velthuis("saṃskṛtam", "sa.msk.rtam"); + test_encoding_to_velthuis("tantraśāstram", "tantra\"saastram"); + test_encoding_to_velthuis("Ā - Ī - Ṝ", "Aa - Ii - .Rr"); + test_encoding_to_velthuis("atha prathamo'dhyāyaḥ", "atha prathamo.adhyaaya.h"); } END_TEST void register_velthuis_encoder_tests(TCase *test_case) { - tcase_add_test(test_case, test_encode_punctation); + tcase_add_test(test_case, test_encode_velthuis_to_iast); + tcase_add_test(test_case, test_encode_iast_to_velthuis); } diff --git a/velthuis.c b/velthuis.c index 7a129c6..ab5bc35 100644 --- a/velthuis.c +++ b/velthuis.c @@ -34,12 +34,12 @@ static const struct encoder_tuple table[] = { {".n", "\u1e47"}, {".N", "\u1e46"}, {".s", "\u1e63"}, {".S", "\u1e62"}, - {"/", "m\u0310"} + {"/", "m\u0310"}, {".a", "'"} }; static const struct encoder_tuple *find_tuple(const char *text) { - int i; + unsigned int i; for (i = 0; i < ARRAY_SIZE(table); i++) { if (strncmp(text, table[i].from, strlen(table[i].from)) == 0) { @@ -50,7 +50,20 @@ static const struct encoder_tuple *find_tuple(const char *text) return NULL; } -int encode_velthuis_to_iast_punctation(const char *text, char **out) +static const struct encoder_tuple *find_tuple_reverse(const char *text) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(table); i++) { + if (strncmp(text, table[i].to, strlen(table[i].to)) == 0) { + return &table[i]; + } + } + + return NULL; +} + +int encode_velthuis_to_iast(const char *text, char **out) { const char *str = text, *end = str + strlen(str); const struct encoder_tuple *tuple; @@ -78,3 +91,32 @@ int encode_velthuis_to_iast_punctation(const char *text, char **out) return 0; } + +int encode_iast_to_velthuis(const char *text, char **out) +{ + const char *str = text, *end = str + strlen(str); + const struct encoder_tuple *tuple; + char *buf, *dest; + + buf = calloc(1, strlen(text) * 2); /* should be enough */ + if (buf == NULL) + return ENOMEM; + + dest = buf; + while (str < end) { + tuple = find_tuple_reverse(str); + if (tuple) { + sprintf(dest, "%s", tuple->from); + str += strlen(tuple->to); + dest += strlen(tuple->from); + } else { + sprintf(dest, "%c", *str); + str++; + dest++; + } + } + + *out = buf; + + return 0; +} diff --git a/velthuis.h b/velthuis.h index 314b99e..22691a0 100644 --- a/velthuis.h +++ b/velthuis.h @@ -3,6 +3,7 @@ #ifndef __VELTHUIS_H #define __VELTHUIS_H -int encode_velthuis_to_iast_punctation(const char *text, char **out); +int encode_velthuis_to_iast(const char *text, char **out); +int encode_iast_to_velthuis(const char *text, char **out); #endif /* __VELTHUIS_H */