Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename match search #112

Merged
merged 6 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ set(SOURCE_FILES
src/include/omega_edit/change.h src/lib/change.cpp src/lib/impl_/change_def.hpp
src/include/omega_edit/check.h src/lib/check.cpp
src/include/omega_edit/edit.h src/lib/edit.cpp
src/include/omega_edit/match.h src/lib/match.cpp
src/include/omega_edit/search.h src/lib/search.cpp
src/include/omega_edit/visit.h src/lib/visit.cpp
src/include/omega_edit/session.h src/lib/session.cpp src/lib/impl_/session_def.hpp
src/include/omega_edit/viewport.h src/lib/viewport.cpp src/lib/impl_/viewport_def.hpp
src/include/omega_edit/license.h src/lib/license.c
src/include/omega_edit/utility.h src/lib/utility.c
src/include/omega_edit/encodings.h src/lib/encodings.c
src/lib/impl_/internal_fun.hpp src/lib/impl_/internal_fun.cpp
src/lib/impl_/search.h src/lib/impl_/search.cpp
src/lib/impl_/find.h src/lib/impl_/find.cpp
src/lib/impl_/data_def.hpp src/lib/impl_/data_segment_def.hpp
src/lib/impl_/model_def.hpp src/lib/impl_/model_segment_def.hpp
src/lib/impl_/macros.hpp
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Ωedit Library
<img alt="Omega Edit Logo" src="https://raw.githubusercontent.com/scholarsmate/omega-edit/main/images/OmegaEditLogo.png" width=64 style="float: left">

![Build Status](https://github.com/scholarsmate/omega-edit/workflows/Unit%20Tests/badge.svg)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgh.neting.cc%2Fscholarsmate%2Fomega-edit.svg?type=shield)](https://app.fossa.com/projects/git%2Bgh.neting.cc%2Fscholarsmate%2Fomega-edit?ref=badge_shield)
[![codecov](https://codecov.io/gh/scholarsmate/omega-edit/branch/main/graph/badge.svg)](https://codecov.io/gh/scholarsmate/omega-edit)

<img alt="Omega Edit Logo" src="https://raw.githubusercontent.com/scholarsmate/omega-edit/main/images/OmegaEditLogo.png" width=64 style="float: left">
The goal of this project is to provide an open source library for building editors that can handle massive files, multiple authors, and multiple viewports.

## Requirements
Expand Down
4 changes: 2 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"src/lib/edit.cpp",
"src/lib/encodings.c",
"src/lib/license.c",
"src/lib/match.cpp",
"src/lib/search.cpp",
"src/lib/session.cpp",
"src/lib/utility.c",
"src/lib/viewport.cpp",
"src/lib/visit.cpp",
"src/lib/impl_/find.cpp",
"src/lib/impl_/internal_fun.cpp",
"src/lib/impl_/search.cpp",
"src/bindings/omega_edit_wrap.cxx"
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/omega_edit.i
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
%include "../include/omega_edit/config.h"
%include "../include/omega_edit/edit.h"
%include "../include/omega_edit/license.h"
%include "../include/omega_edit/match.h"
%include "../include/omega_edit/search.h"
%include "../include/omega_edit/session.h"
%include "../include/omega_edit/stl_string_adaptor.hpp"
%include "../include/omega_edit/viewport.h"
Expand Down
214 changes: 107 additions & 107 deletions src/bindings/omega_edit_wrap.cxx

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/examples/omega_replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pattern = process.argv[4]
replacement = process.argv[5]
session = omega_edit.omega_edit_create_session(in_filename, null, null)
console.assert(session != null, {errorMsg: "session creation failed"})
match_context = omega_edit.omega_match_create_context_string(session, pattern)
console.assert(match_context != null, {errorMsg: "match context creation failed"})
search_context = omega_edit.omega_search_create_context_string(session, pattern)
console.assert(search_context != null, {errorMsg: "match context creation failed"})
replacements = 0
if (omega_edit.omega_match_find(match_context, 1)) {
if (omega_edit.omega_search_next_match(search_context, 1)) {
do {
pattern_offset = omega_edit.omega_match_context_get_offset(match_context)
pattern_offset = omega_edit.omega_search_context_get_offset(search_context)
if (pattern.length == replacement.length) {
omega_edit.omega_edit_overwrite_string(session, pattern_offset, replacement)
} else {
Expand All @@ -38,9 +38,9 @@ if (omega_edit.omega_match_find(match_context, 1)) {
omega_edit.omega_edit_insert_string(session, pattern_offset, replacement)
}
++replacements
} while (omega_edit.omega_match_find(match_context, replacement.length));
} while (omega_edit.omega_search_next_match(search_context, replacement.length));
}
omega_edit.omega_match_destroy_context(match_context)
omega_edit.omega_search_destroy_context(search_context)
rc = omega_edit.omega_edit_save(session, out_filename)
console.assert(rc === 0, {rc: rc, errorMsg: "save failed"})
console.log("Replaced " + replacements + " instances using " + omega_edit.omega_session_get_num_changes(session) + " changes.")
Expand Down
13 changes: 7 additions & 6 deletions src/examples/replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ int main(int arc, char **argv) {
const string replacement = argv[4];
auto session_ptr = omega_scoped_ptr<omega_session_t>(omega_edit_create_session(in_filename, nullptr, nullptr),
omega_edit_destroy_session);
auto match_context_ptr = omega_scoped_ptr<omega_match_context_t>(
omega_match_create_context_string(session_ptr.get(), argv[3]), omega_match_destroy_context);
const auto pattern_length = omega_match_context_get_length(match_context_ptr.get());
if (omega_match_find(match_context_ptr.get(), 1)) {
auto match_context_ptr = omega_scoped_ptr<omega_search_context_t>(
omega_search_create_context_string(session_ptr.get(), argv[3]), omega_search_destroy_context);
const auto pattern_length = omega_search_context_get_length(match_context_ptr.get());
if (omega_search_next_match(match_context_ptr.get(), 1)) {
const auto replacement_length = static_cast<int64_t>(replacement.length());
do {
const auto pattern_offset = omega_match_context_get_offset(match_context_ptr.get());
const auto pattern_offset = omega_search_context_get_offset(match_context_ptr.get());
if (pattern_length == replacement_length) {
// pattern length matches the replacement length, so a single overwrite is sufficient
if (0 >= omega_edit_overwrite_string(session_ptr.get(), pattern_offset, replacement)) {
Expand All @@ -67,7 +67,8 @@ int main(int arc, char **argv) {
}
}
++replacements;
} while (omega_match_find(match_context_ptr.get(), replacement_length));//advance find by the replacement length
} while (omega_search_next_match(match_context_ptr.get(),
replacement_length));//advance find by the replacement length
}
if (0 != omega_edit_save(session_ptr.get(), argv[2])) {
cerr << "Error saving session to " << argv[2] << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/include/omega_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "omega_edit/change.h"
#include "omega_edit/edit.h"
#include "omega_edit/license.h"
#include "omega_edit/match.h"
#include "omega_edit/search.h"
#include "omega_edit/session.h"
#include "omega_edit/viewport.h"
#include "omega_edit/visit.h"
Expand Down
63 changes: 32 additions & 31 deletions src/include/omega_edit/match.h → src/include/omega_edit/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* *
**********************************************************************************************************************/

#ifndef OMEGA_EDIT_MATCH_H
#define OMEGA_EDIT_MATCH_H
#ifndef OMEGA_EDIT_SEARCH_H
#define OMEGA_EDIT_SEARCH_H

#include "byte.h"
#include "fwd_defs.h"
Expand All @@ -26,12 +26,12 @@ extern "C" {
#endif

/**
* Opaque match context
* Opaque search context
*/
typedef struct omega_match_context_t omega_match_context_t;
typedef struct omega_search_context_t omega_search_context_t;

/**
* Create a match context
* Create a search context
* @param session_ptr session to find patterns in
* @param pattern pointer to the pattern to find (as a sequence of bytes)
* @param pattern_length length of the pattern (if 0, strlen will be used to calculate the length of null-terminated
Expand All @@ -40,61 +40,62 @@ typedef struct omega_match_context_t omega_match_context_t;
* @param session_length search from the starting offset within the session up to this many bytes, if set to zero, it
* will track the computed session length
* @param case_insensitive zero for case sensitive match and non-zero otherwise
* @return match context
* @return search context
*/
omega_match_context_t *omega_match_create_context_bytes(const omega_session_t *session_ptr, const omega_byte_t *pattern,
int64_t pattern_length, int64_t session_offset,
int64_t session_length, int case_insensitive);
omega_search_context_t *omega_search_create_context_bytes(const omega_session_t *session_ptr,
const omega_byte_t *pattern, int64_t pattern_length,
int64_t session_offset, int64_t session_length,
int case_insensitive);

/**
* Create a match context
* Create a search context
* @param session_ptr session to find patterns in
* @param pattern pointer to the pattern to find (as a C string)
* @param pattern_length length of the pattern (if 0, strlen will be used to calculate the length of null-terminated
* bytes)
* @param session_offset start searching at this offset within the session
* @param session_length search from the starting offset within the session up to this many bytes, if set to zero, it
* will track the computed session length
* @param case_insensitive zero for case sensitive match and non-zero otherwise
* @return match context
* @param case_insensitive zero for case sensitive matching and non-zero otherwise
* @return search context
*/
inline omega_match_context_t *omega_match_create_context(const omega_session_t *session_ptr, const char *pattern,
int64_t pattern_length, int64_t session_offset,
int64_t session_length, int case_insensitive) {
return omega_match_create_context_bytes(session_ptr, (const omega_byte_t *) pattern, pattern_length, session_offset,
session_length, case_insensitive);
inline omega_search_context_t *omega_search_create_context(const omega_session_t *session_ptr, const char *pattern,
int64_t pattern_length, int64_t session_offset,
int64_t session_length, int case_insensitive) {
return omega_search_create_context_bytes(session_ptr, (const omega_byte_t *) pattern, pattern_length,
session_offset, session_length, case_insensitive);
}

/**
* Given a match context, get the most recent match offset
* @param match_context_ptr match context to get the most recent match offset from
* @return the most recent match offset, if the match offset is equal to the session length, then no match was found
* Given a search context, get the most recent search offset
* @param search_context_ptr search context to get the most recent search offset from
* @return the most recent search offset, if the search offset is equal to the session length, then no match was found
*/
int64_t omega_match_context_get_offset(const omega_match_context_t *match_context_ptr);
int64_t omega_search_context_get_offset(const omega_search_context_t *search_context_ptr);

/**
* Given a match context, get the pattern length
* @param match_context_ptr match context to get the pattern length from
* Given a search context, get the pattern length
* @param search_context_ptr search context to get the pattern length from
* @return the pattern length offset
*/
int64_t omega_match_context_get_length(const omega_match_context_t *match_context_ptr);
int64_t omega_search_context_get_length(const omega_search_context_t *search_context_ptr);

/**
* Given a match context, find the next match
* @param match_context_ptr match context to find the next match in
* @param advance_context advance the internal matching context by this many bytes
* Given a search context, find the next match
* @param search_context_ptr search context to find the next match in
* @param advance_context advance the internal search context offset by this many bytes
* @return non-zero if a match is found, zero otherwise
*/
int omega_match_find(omega_match_context_t *match_context_ptr, int64_t advance_context);
int omega_search_next_match(omega_search_context_t *search_context_ptr, int64_t advance_context);

/**
* Destroy the given search context
* @param match_context_ptr match context to destroy
* @param search_context_ptr search context to destroy
*/
void omega_match_destroy_context(omega_match_context_t *match_context_ptr);
void omega_search_destroy_context(omega_search_context_t *search_context_ptr);

#ifdef __cplusplus
}
#endif

#endif//OMEGA_EDIT_MATCH_H
#endif//OMEGA_EDIT_SEARCH_H
19 changes: 10 additions & 9 deletions src/include/omega_edit/stl_string_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "change.h"
#include "edit.h"
#include "match.h"
#include "search.h"
#include "viewport.h"
#include <string>

Expand Down Expand Up @@ -69,20 +69,21 @@ inline int64_t omega_edit_overwrite_string(omega_session_t *session_ptr, int64_t
}

/**
* Create a match context
* Create a search context
* @param session_ptr session to find patterns in
* @param pattern pattern string to find
* @param session_offset start searching at this offset within the session
* @param session_length search from the starting offset within the session up to this many bytes, if set to zero, it
* will track the computed session length
* @param case_insensitive zero for case sensitive match and non-zero otherwise
* @return match context
* @param case_insensitive zero for case sensitive matching and non-zero otherwise
* @return search context
*/
inline omega_match_context_t *omega_match_create_context_string(const omega_session_t *session_ptr,
const std::string &pattern, int64_t session_offset = 0,
int64_t session_length = 0, int case_insensitive = 0) {
return omega_match_create_context(session_ptr, pattern.c_str(), static_cast<int64_t>(pattern.length()),
session_offset, session_length, case_insensitive);
inline omega_search_context_t *
omega_search_create_context_string(const omega_session_t *session_ptr,
const std::string &pattern, int64_t session_offset = 0,
int64_t session_length = 0, int case_insensitive = 0) {
return omega_search_create_context(session_ptr, pattern.c_str(), static_cast<int64_t>(pattern.length()),
session_offset, session_length, case_insensitive);
}

#endif//__cplusplus
Expand Down
18 changes: 9 additions & 9 deletions src/lib/impl_/search.cpp → src/lib/impl_/find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
* *
**********************************************************************************************************************/

#include "search.h"
#include "find.h"
#include <cassert>
#include <climits>
#include <cstring>
#include <vector>

struct omega_search_skip_table_t : public std::vector<size_t> {
omega_search_skip_table_t(size_t vec_size, size_t fill) : std::vector<size_t>(vec_size, fill) {}
struct omega_find_skip_table_t : public std::vector<size_t> {
omega_find_skip_table_t(size_t vec_size, size_t fill) : std::vector<size_t>(vec_size, fill) {}
};

const omega_search_skip_table_t *omega_search_create_skip_table(const unsigned char *needle, size_t needle_length) {
const omega_find_skip_table_t *omega_find_create_skip_table(const unsigned char *needle, size_t needle_length) {
assert(needle);
assert(needle_length > 0);
auto skip_table_ptr = new omega_search_skip_table_t(UCHAR_MAX + 1, needle_length);
auto skip_table_ptr = new omega_find_skip_table_t(UCHAR_MAX + 1, needle_length);
assert(skip_table_ptr);
if (needle_length >= 1) {
const auto needle_length_minus_1 = needle_length - 1;
Expand All @@ -37,9 +37,9 @@ const omega_search_skip_table_t *omega_search_create_skip_table(const unsigned c
/*
* Boyer-Moore-Horspool with additional tuning (https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.7176)
*/
const unsigned char *omega_search(const unsigned char *haystack, size_t haystack_length,
const omega_search_skip_table_t *skip_table_ptr, const unsigned char *needle,
size_t needle_length) {
const unsigned char *omega_find(const unsigned char *haystack, size_t haystack_length,
const omega_find_skip_table_t *skip_table_ptr, const unsigned char *needle,
size_t needle_length) {
assert(haystack);
assert(skip_table_ptr);
assert(needle);
Expand All @@ -62,7 +62,7 @@ const unsigned char *omega_search(const unsigned char *haystack, size_t haystack
return nullptr;
}

void omega_search_destroy_skip_table(const omega_search_skip_table_t *skip_table_ptr) {
void omega_find_destroy_skip_table(const omega_find_skip_table_t *skip_table_ptr) {
assert(skip_table_ptr);
delete skip_table_ptr;
}
26 changes: 13 additions & 13 deletions src/lib/impl_/search.h → src/lib/impl_/find.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* *
**********************************************************************************************************************/

#ifndef OMEGA_EDIT_SEARCH_H
#define OMEGA_EDIT_SEARCH_H
#ifndef OMEGA_EDIT_FIND_H
#define OMEGA_EDIT_FIND_H

#ifdef __cplusplus
#include <cstddef>
Expand All @@ -22,37 +22,37 @@ extern "C" {
#include <stddef.h>
#endif

struct omega_search_skip_table_t;
struct omega_find_skip_table_t;

/**
* Preprocess the needle to create a skip table for use in the omega_search function
* Preprocess the needle to create a skip table for use in the omega_find function
* @param needle needle to process
* @param needle_length length of the needle to process
* @return skip table for use in the omega_search function
* @return skip table for use in the omega_find function
*/
const omega_search_skip_table_t *omega_search_create_skip_table(const unsigned char *needle, size_t needle_length);
const omega_find_skip_table_t *omega_find_create_skip_table(const unsigned char *needle, size_t needle_length);

/**
* Finds the first offset in the haystack where the needle is found, otherwise, return haystack_length
* @param haystack haystack to search in
* @param haystack_length length of haystack
* @param skip_table_ptr skip table for this needle, created using the omega_search_create_skip_table function
* @param skip_table_ptr skip table for this needle, created using the omega_find_create_skip_table function
* @param needle needle to find
* @param needle_length length of needle to find
* @return first offset in the haystack where the needle was found, or haystack length
*/
const unsigned char *omega_search(const unsigned char *haystack, size_t haystack_length,
const omega_search_skip_table_t *skip_table_ptr, const unsigned char *needle,
size_t needle_length);
const unsigned char *omega_find(const unsigned char *haystack, size_t haystack_length,
const omega_find_skip_table_t *skip_table_ptr, const unsigned char *needle,
size_t needle_length);

/**
* Destroys a skip table created by omega_search_create_skip_table
* Destroys a skip table created by omega_find_create_skip_table
* @param skip_table_ptr skip table to destroy
*/
void omega_search_destroy_skip_table(const omega_search_skip_table_t *skip_table_ptr);
void omega_find_destroy_skip_table(const omega_find_skip_table_t *skip_table_ptr);

#ifdef __cplusplus
}
#endif

#endif//OMEGA_EDIT_SEARCH_H
#endif//OMEGA_EDIT_FIND_H
Loading