Repository: SINTEF/dlite (language: python)

Issue:
Add "str" as alias for "string" in datamodel types
I see that people writes tends to write "str" as a natural abbreviation for "string". 
We already have "int" as an abbreviation for "integer" so, adding "str" seems natural.




Discussion / hints:

Fix patch:
diff --git a/src/dlite-type.c b/src/dlite-type.c
index 9faa6f258..c9c5d1f3d 100644
--- a/src/dlite-type.c
+++ b/src/dlite-type.c
@@ -89,6 +89,7 @@ static struct _TypeDescr {
   {"float128", dliteFloat,     16,                    alignof(float128_t)},
 #endif
   {"string",   dliteStringPtr, sizeof(char *),        alignof(char *)},
+  {"str",      dliteStringPtr, sizeof(char *),        alignof(char *)},
   {"ref",      dliteRef,       sizeof(DLiteRef),      alignof(DLiteRef)},
   {"dimension",dliteDimension, sizeof(DLiteDimension),alignof(DLiteDimension)},
   {"property", dliteProperty,  sizeof(DLiteProperty), alignof(DLiteProperty)},
@@ -516,7 +517,8 @@ int dlite_type_set_dtype_and_size(const char *typename,
   if (strncmp(typename, "blob", namelen) == 0) {
     *dtype = dliteBlob;
     *size = typesize;
-  } else if (strncmp(typename, "string", namelen) == 0) {
+  } else if (strncmp(typename, "string", namelen) == 0 ||
+             strncmp(typename, "str", namelen)) {
     *dtype = dliteFixString;
     *size = typesize+1;
   } else {


Test patch:
diff --git a/src/tests/test_type.c b/src/tests/test_type.c
index 9fd92503d..1fb5314f0 100644
--- a/src/tests/test_type.c
+++ b/src/tests/test_type.c
@@ -116,10 +116,18 @@ MU_TEST(test_set_dtype_and_size)
   mu_assert_int_eq(dliteFixString, type);
   mu_assert_int_eq(11, size);
 
+  mu_assert_int_eq(0, dlite_type_set_dtype_and_size("str5", &type, &size));
+  mu_assert_int_eq(dliteFixString, type);
+  mu_assert_int_eq(6, size);
+
   mu_assert_int_eq(0, dlite_type_set_dtype_and_size("string", &type, &size));
   mu_assert_int_eq(dliteStringPtr, type);
   mu_assert_int_eq(sizeof(char *), size);
 
+  mu_assert_int_eq(0, dlite_type_set_dtype_and_size("str", &type, &size));
+  mu_assert_int_eq(dliteStringPtr, type);
+  mu_assert_int_eq(sizeof(char *), size);
+
   mu_assert_int_eq(0, dlite_type_set_dtype_and_size("ref", &type, &size));
   mu_assert_int_eq(dliteRef, type);
   mu_assert_int_eq(sizeof(DLiteInstance *), size);
@@ -136,14 +144,18 @@ MU_TEST(test_set_dtype_and_size)
   mu_assert_int_eq(dliteProperty, type);
   mu_assert_int_eq(sizeof(DLiteProperty), size);
 
-  // ok with comma following the type string
+  // ok with comma or space following the type string
   mu_assert_int_eq(0, dlite_type_set_dtype_and_size("string8,", &type, &size));
   mu_assert_int_eq(dliteFixString, type);
   mu_assert_int_eq(9, size);
 
+  mu_assert_int_eq(0, dlite_type_set_dtype_and_size("string6 abc", &type, &size));
+  mu_assert_int_eq(dliteFixString, type);
+  mu_assert_int_eq(7, size);
+
   mu_check(dlite_type_set_dtype_and_size("blob5a", &type, &size)); // fails
   mu_assert_int_eq(dliteFixString, type);
-  mu_assert_int_eq(9, size);  // unchanged
+  mu_assert_int_eq(7, size);  // unchanged
   err_clear();
 }