Quellcode durchsuchen

LibC: Implement wctype

Tim Schumacher vor 3 Jahren
Ursprung
Commit
7b17230d7a
3 geänderte Dateien mit 86 neuen und 3 gelöschten Zeilen
  1. 1 0
      Tests/LibC/CMakeLists.txt
  2. 30 0
      Tests/LibC/TestWctype.cpp
  3. 55 3
      Userland/Libraries/LibC/wctype.cpp

+ 1 - 0
Tests/LibC/CMakeLists.txt

@@ -15,6 +15,7 @@ set(TEST_SOURCES
     TestStackSmash.cpp
     TestStrlcpy.cpp
     TestStrtodAccuracy.cpp
+    TestWctype.cpp
 )
 
 set_source_files_properties(TestStrtodAccuracy.cpp PROPERTIES COMPILE_FLAGS "-fno-builtin-strtod")

+ 30 - 0
Tests/LibC/TestWctype.cpp

@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2021, the SerenityOS developers
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+
+#include <wctype.h>
+
+TEST_CASE(wctype)
+{
+    // Test that existing properties return non-zero wctypes.
+    EXPECT(wctype("alnum") != 0);
+    EXPECT(wctype("alpha") != 0);
+    EXPECT(wctype("blank") != 0);
+    EXPECT(wctype("cntrl") != 0);
+    EXPECT(wctype("digit") != 0);
+    EXPECT(wctype("graph") != 0);
+    EXPECT(wctype("lower") != 0);
+    EXPECT(wctype("print") != 0);
+    EXPECT(wctype("punct") != 0);
+    EXPECT(wctype("space") != 0);
+    EXPECT(wctype("upper") != 0);
+    EXPECT(wctype("xdigit") != 0);
+
+    // Test that invalid properties return the "invalid" wctype (0).
+    EXPECT(wctype("") == 0);
+    EXPECT(wctype("abc") == 0);
+}

+ 55 - 3
Userland/Libraries/LibC/wctype.cpp

@@ -5,8 +5,25 @@
  */
 
 #include <AK/Format.h>
+#include <string.h>
 #include <wctype.h>
 
+enum {
+    WCTYPE_INVALID = 0,
+    WCTYPE_ALNUM,
+    WCTYPE_ALPHA,
+    WCTYPE_BLANK,
+    WCTYPE_CNTRL,
+    WCTYPE_DIGIT,
+    WCTYPE_GRAPH,
+    WCTYPE_LOWER,
+    WCTYPE_PRINT,
+    WCTYPE_PUNCT,
+    WCTYPE_SPACE,
+    WCTYPE_UPPER,
+    WCTYPE_XDIGIT,
+};
+
 extern "C" {
 
 int iswalnum(wint_t wc)
@@ -75,10 +92,45 @@ int iswctype(wint_t, wctype_t)
     TODO();
 }
 
-wctype_t wctype(const char*)
+wctype_t wctype(const char* property)
 {
-    dbgln("FIXME: Implement wctype()");
-    TODO();
+    if (strcmp(property, "alnum") == 0)
+        return WCTYPE_ALNUM;
+
+    if (strcmp(property, "alpha") == 0)
+        return WCTYPE_ALPHA;
+
+    if (strcmp(property, "blank") == 0)
+        return WCTYPE_BLANK;
+
+    if (strcmp(property, "cntrl") == 0)
+        return WCTYPE_CNTRL;
+
+    if (strcmp(property, "digit") == 0)
+        return WCTYPE_DIGIT;
+
+    if (strcmp(property, "graph") == 0)
+        return WCTYPE_GRAPH;
+
+    if (strcmp(property, "lower") == 0)
+        return WCTYPE_LOWER;
+
+    if (strcmp(property, "print") == 0)
+        return WCTYPE_PRINT;
+
+    if (strcmp(property, "punct") == 0)
+        return WCTYPE_PUNCT;
+
+    if (strcmp(property, "space") == 0)
+        return WCTYPE_SPACE;
+
+    if (strcmp(property, "upper") == 0)
+        return WCTYPE_UPPER;
+
+    if (strcmp(property, "xdigit") == 0)
+        return WCTYPE_XDIGIT;
+
+    return WCTYPE_INVALID;
 }
 
 wint_t towlower(wint_t wc)