groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 10/13: [libgroff]: Add `find()` to `string` class.


From: G. Branden Robinson
Subject: [groff] 10/13: [libgroff]: Add `find()` to `string` class.
Date: Mon, 7 Oct 2024 08:00:15 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 5ec167ea43dbf8c8095cb46140083dc6f9d04d6e
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Sun Oct 6 12:06:13 2024 -0500

    [libgroff]: Add `find()` to `string` class.
    
    * src/include/stringclass.h (class string): Declare new `find` public
      member function; it works like `search` but takes a pointer to `const
      char`, making it a substring locator.
    
    * src/libs/libgroff/string.cpp (string::find): Implement it.
---
 ChangeLog                    |  8 ++++++++
 src/include/stringclass.h    |  1 +
 src/libs/libgroff/string.cpp | 12 +++++++++++-
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index fc9a987ef..efc768219 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-10-06  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/include/stringclass.h (class string): Declare new `find`
+       public member function; it works like `search` but takes a
+       pointer to `const char`, making it a substring locator.
+
+       * src/libs/libgroff/string.cpp (string::find): Implement it.
+
 2024-10-06  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * bootstrap.conf: Add "memmem" to `gnulib_modules`.
diff --git a/src/include/stringclass.h b/src/include/stringclass.h
index 6889c41a9..f59826636 100644
--- a/src/include/stringclass.h
+++ b/src/include/stringclass.h
@@ -68,6 +68,7 @@ public:
   void set_length(int i);
   const char *contents() const;
   int search(const char) const;
+  int find(const char *) const;
   char *extract() const;
   void remove_spaces();
   void clear();
diff --git a/src/libs/libgroff/string.cpp b/src/libs/libgroff/string.cpp
index d1bd97243..c7462109c 100644
--- a/src/libs/libgroff/string.cpp
+++ b/src/libs/libgroff/string.cpp
@@ -1,4 +1,4 @@
-/* Copyright (C) 1989-2020 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2024 Free Software Foundation, Inc.
      Written by James Clark (jjc@jclark.com)
 
 This file is part of groff.
@@ -284,6 +284,16 @@ int string::search(const char c) const
   return (p != 0 /* nullptr */) ? (p - ptr) : -1;
 }
 
+// Return index of substing `c` in string, -1 if not found.
+int string::find(const char *c) const
+{
+  const char *p = ptr
+                 ? static_cast<const char *>(memmem(ptr, len, c,
+                                                    strlen(c)))
+                 : 0  /* nullptr */;
+  return (p != 0 /* nullptr */) ? (p - ptr) : -1;
+}
+
 // we silently strip nuls
 
 char *string::extract() const



reply via email to

[Prev in Thread] Current Thread [Next in Thread]