]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcstr.c
Fix "contains" op for strings and langsets.
[fontconfig.git] / src / fcstr.c
index 940bf49aa377b025bc2299a1b6fb4b83628cb5b0..e67563dcf55bef38db8e98d95b26977a09983ed3 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $XFree86: xc/lib/fontconfig/src/fcstr.c,v 1.7 2002/07/13 05:43:25 keithp Exp $
+ * $RCSId: xc/lib/fontconfig/src/fcstr.c,v 1.10 2002/08/31 22:17:32 keithp Exp $
  *
- * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
+ * Copyright © 2000 Keith Packard
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -72,11 +72,7 @@ FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
     {
        c1 = *s1++;
        c2 = *s2++;
-       if (!c1 || !c2)
-           break;
-       c1 = FcToLower (c1);
-       c2 = FcToLower (c2);
-       if (c1 != c2)
+       if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
            break;
     }
     return (int) c1 - (int) c2;
@@ -95,11 +91,7 @@ FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
        do
            c2 = *s2++;
        while (c2 == ' ');
-       if (!c1 || !c2)
-           break;
-       c1 = FcToLower (c1);
-       c2 = FcToLower (c2);
-       if (c1 != c2)
+       if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
            break;
     }
     return (int) c1 - (int) c2;
@@ -116,23 +108,198 @@ FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
     {
        c1 = *s1++;
        c2 = *s2++;
-       if (!c1 || !c2)
-           break;
-       if (c1 != c2)
+       if (!c1 || c1 != c2)
            break;
     }
     return (int) c1 - (int) c2;
 }
 
+/*
+ * Is the head of s1 equal to s2?
+ */
+
+static FcBool
+FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
+{
+    FcChar8 c1, c2;
+    
+    for (;;) 
+    {
+       do
+           c1 = *s1++;
+       while (c1 == ' ');
+       do
+           c2 = *s2++;
+       while (c2 == ' ');
+       if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
+           break;
+    }
+    return c1 == c2 || !c2;
+}
+
+/*
+ * Does s1 contain an instance of s2 (ignoring blanks and case)?
+ */
+
+const FcChar8 *
+FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
+{
+    while (*s1)
+    {
+       if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
+           return s1;
+       s1++;
+    }
+    return 0;
+}
+
+/*
+ * Is the head of s1 equal to s2?
+ */
+
+static FcBool
+FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
+{
+    FcChar8 c1, c2;
+    
+    for (;;) 
+    {
+       c1 = *s1++;
+       c2 = *s2++;
+       if (!c1 || (c1 != c2 && (c1 = FcToLower(c1)) != (c2 = FcToLower(c2))))
+           break;
+    }
+    return c1 == c2 || !c2;
+}
+
+/*
+ * Does s1 contain an instance of s2 (ignoring blanks and case)?
+ */
+
+const FcChar8 *
+FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
+{
+    while (*s1)
+    {
+       if (FcStrIsAtIgnoreCase (s1, s2))
+           return s1;
+       s1++;
+    }
+    return 0;
+}
+
+const FcChar8 *
+FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
+{
+    FcChar8 c1, c2;
+    const FcChar8 * p = s1;
+    const FcChar8 * b = s2;
+
+    if (!s1 || !s2)
+       return 0;
+
+    if (s1 == s2)
+       return s1;
+
+again:
+    c2 = *s2++;
+    c2 = FcToLower (c2);
+
+    if (!c2)
+       return 0;
+
+    for (;;) 
+    {
+       p = s1;
+       c1 = *s1++;
+       if (!c1 || (c1 = FcToLower (c1)) == c2)
+           break;
+    }
+
+    if (c1 != c2)
+       return 0;
+
+    for (;;)
+    {
+       c1 = *s1;
+       c2 = *s2;
+       if (c1 && c2 && (c1 = FcToLower (c1)) != (c2 = FcToLower (c2)))
+       {
+           s1 = p + 1;
+           s2 = b;
+           goto again;
+       }
+       if (!c2)
+           return p;
+       if (!c1)
+           return 0;
+       ++ s1;
+       ++ s2;
+    }
+
+    return 0;
+}
+
+const FcChar8 *
+FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
+{
+    FcChar8 c1, c2;
+    const FcChar8 * p = s1;
+    const FcChar8 * b = s2;
+
+    if (!s1 || !s2)
+       return 0;
+
+    if (s1 == s2)
+       return s1;
+
+again:
+    c2 = *s2++;
+
+    if (!c2)
+       return 0;
+
+    for (;;) 
+    {
+       p = s1;
+       c1 = *s1++;
+       if (!c1 || c1 == c2)
+           break;
+    }
+
+    if (c1 != c2)
+       return 0;
+
+    for (;;)
+    {
+       c1 = *s1;
+       c2 = *s2;
+       if (c1 && c2 && c1 != c2)
+       {
+           s1 = p + 1;
+           s2 = b;
+           goto again;
+       }
+       if (!c2)
+           return p;
+       if (!c1)
+           return 0;
+       ++ s1;
+       ++ s2;
+    }
+
+    return 0;
+}
+
 int
-FcUtf8ToUcs4 (FcChar8   *src_orig,
-             FcChar32  *dst,
-             int       len)
+FcUtf8ToUcs4 (const FcChar8 *src_orig,
+             FcChar32      *dst,
+             int           len)
 {
-    FcChar8    *src = src_orig;
-    FcChar8    s;
-    int                extra;
-    FcChar32   result;
+    const FcChar8   *src = src_orig;
+    FcChar8        s;
+    int                    extra;
+    FcChar32       result;
 
     if (len == 0)
        return 0;
@@ -196,10 +363,10 @@ FcUtf8ToUcs4 (FcChar8   *src_orig,
 }
 
 FcBool
-FcUtf8Len (FcChar8     *string,
-           int         len,
-           int         *nchar,
-           int         *wchar)
+FcUtf8Len (const FcChar8    *string,
+          int              len,
+          int              *nchar,
+          int              *wchar)
 {
     int                n;
     int                clen;
@@ -255,14 +422,14 @@ FcUcs4ToUtf8 (FcChar32    ucs4,
      (FcChar16) ((src)[endian == FcEndianBig ? 1 : 0]))
 
 int
-FcUtf16ToUcs4 (FcChar8 *src_orig,
-              FcEndian endian,
-              FcChar32 *dst,
-              int      len)    /* in bytes */
+FcUtf16ToUcs4 (const FcChar8   *src_orig,
+              FcEndian         endian,
+              FcChar32         *dst,
+              int              len)    /* in bytes */
 {
-    FcChar8    *src = src_orig;
-    FcChar16   a, b;
-    FcChar32   result;
+    const FcChar8   *src = src_orig;
+    FcChar16       a, b;
+    FcChar32       result;
 
     if (len < 2)
        return 0;
@@ -283,7 +450,7 @@ FcUtf16ToUcs4 (FcChar8      *src_orig,
        if ((b & 0xfc00) != 0xdc00)
            return 0;
        result = ((((FcChar32) a & 0x3ff) << 10) |
-                 ((FcChar32) b & 0x3ff)) | 0x10000;
+                 ((FcChar32) b & 0x3ff)) + 0x10000;
     }
     else
        result = a;
@@ -292,11 +459,11 @@ FcUtf16ToUcs4 (FcChar8    *src_orig,
 }
 
 FcBool
-FcUtf16Len (FcChar8    *string,
-           FcEndian    endian,
-           int         len,    /* in bytes */
-           int         *nchar,
-           int         *wchar)
+FcUtf16Len (const FcChar8   *string,
+           FcEndian        endian,
+           int             len,        /* in bytes */
+           int             *nchar,
+           int             *wchar)
 {
     int                n;
     int                clen;
@@ -341,6 +508,7 @@ FcStrBufDestroy (FcStrBuf *buf)
 {
     if (buf->allocated)
     {
+       FcMemFree (FC_MEM_STRBUF, buf->size);
        free (buf->buf);
        FcStrBufInit (buf, 0, 0);
     }
@@ -354,6 +522,7 @@ FcStrBufDone (FcStrBuf *buf)
     ret = malloc (buf->len + 1);
     if (ret)
     {
+       FcMemAlloc (FC_MEM_STRING, buf->len + 1);
        memcpy (ret, buf->buf, buf->len);
        ret[buf->len] = '\0';
     }
@@ -389,6 +558,9 @@ FcStrBufChar (FcStrBuf *buf, FcChar8 c)
            buf->failed = FcTrue;
            return FcFalse;
        }
+       if (buf->size)
+           FcMemFree (FC_MEM_STRBUF, buf->size);
+       FcMemAlloc (FC_MEM_STRBUF, size);
        buf->size = size;
        buf->buf = new;
     }
@@ -415,6 +587,12 @@ FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
     return FcTrue;
 }
 
+FcBool
+FcStrUsesHome (const FcChar8 *s)
+{
+    return *s == '~';
+}
+
 FcChar8 *
 FcStrCopyFilename (const FcChar8 *s)
 {
@@ -422,10 +600,11 @@ FcStrCopyFilename (const FcChar8 *s)
     
     if (*s == '~')
     {
-       FcChar8 *home = (FcChar8 *) getenv ("HOME");
-       int     size = strlen ((char *) home) + strlen ((char *) s);
+       FcChar8 *home = FcConfigHome ();
+       int     size;
        if (!home)
            return 0;
+       size = strlen ((char *) home) + strlen ((char *) s);
        new = (FcChar8 *) malloc (size);
        if (!new)
            return 0;
@@ -445,13 +624,32 @@ FcStrCopyFilename (const FcChar8 *s)
     return new;
 }
 
+FcChar8 *
+FcStrLastSlash (const FcChar8  *path)
+{
+    FcChar8        *slash;
+
+    slash = (FcChar8 *) strrchr ((const char *) path, '/');
+#ifdef _WIN32
+    {
+        FcChar8     *backslash;
+
+       backslash = (FcChar8 *) strrchr ((const char *) path, '\\');
+       if (!slash || (backslash && backslash > slash))
+           slash = backslash;
+    }
+#endif
+
+    return slash;
+}
+  
 FcChar8 *
 FcStrDirname (const FcChar8 *file)
 {
     FcChar8 *slash;
     FcChar8 *dir;
 
-    slash = (FcChar8 *) strrchr ((char *) file, '/');
+    slash = FcStrLastSlash (file);
     if (!slash)
        return FcStrCopy ((FcChar8 *) ".");
     dir = malloc ((slash - file) + 1);
@@ -468,7 +666,7 @@ FcStrBasename (const FcChar8 *file)
 {
     FcChar8 *slash;
 
-    slash = (FcChar8 *) strrchr ((char *) file, '/');
+    slash = FcStrLastSlash (file);
     if (!slash)
        return FcStrCopy (file);
     return FcStrCopy (slash + 1);