velthuis.c - make bidirectional

This commit is contained in:
Vlasta Vesely 2021-04-16 10:03:08 +02:00
parent 0931a29649
commit b0cd85bd21
3 changed files with 76 additions and 15 deletions

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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 */