Skip to content

Commit

Permalink
[实现 GlobMatch 功能]: 主要实现了 GlobMatch 功能,并对项目文件进行了相应的更新。
Browse files Browse the repository at this point in the history
- 在项目中添加了 Dependabot 配置文件 `.github/dependabot.yml`,以便自动维护 GitHub Actions 的依赖。
- 更新了 `.github/workflows/build.yml` 文件,移除了不再需要的 `save-always` 配置。
- 在 `CMakeLists.txt` 中添加了 `GlobMatch` 子目录,以支持新功能的构建。
- 添加了 `GlobMatch` 目录和相关文件,包括 `CMakeLists.txt`、`globmatcher.cc`、`globmatcher.hpp` 和 `main.cc`,实现了基于 glob 模式的匹配功能。
- 在 `README.md` 中记录了 `GlobMatch` 功能的添加,并对列表进行了相应的更新。
- 更新了 `vcpkg.json` 文件,以反映项目依赖的变化。
  • Loading branch information
RealChuan committed Nov 28, 2024
1 parent 9869a6c commit cd13cdf
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 14 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"

1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ jobs:
${{ matrix.os }}-vcpkg-installed-${{ runner.os }}-
${{ matrix.os }}-vcpkg-installed-
${{ matrix.os }}-
save-always: true
- name: Configure and build windows
if: startsWith(matrix.os, 'windows')
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ add_subdirectory(ByteOrder)
add_subdirectory(CountDownLatch)
add_subdirectory(Crashpad)
add_subdirectory(Curl)
add_subdirectory(GlobMatch)
add_subdirectory(DesignPattern)
add_subdirectory(Glog)
add_subdirectory(LinkedList)
Expand Down
1 change: 1 addition & 0 deletions GlobMatch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_executable(globmatcher_test globmatcher.cc globmatcher.hpp main.cc)
63 changes: 63 additions & 0 deletions GlobMatch/globmatcher.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "globmatcher.hpp"

#include <fstream>
#include <iostream>

std::vector<std::string> readGlobPatternsFromFile(const std::string &filename)
{
std::vector<std::string> patterns;
std::ifstream file(filename);
std::string pattern;
if (file.is_open()) {
while (std::getline(file, pattern)) {
patterns.push_back(pattern);
}
file.close();
} else {
std::cerr << "Unable to open file: " << filename << std::endl;
}
return patterns;
}

bool globMatch(const std::string &text, const std::string &pattern)
{
size_t n = text.size(), m = pattern.size();
size_t i = 0, j = 0;
while (i < n && j < m) {
if (pattern[j] == '*') {
while (j < m && pattern[j] == '*')
++j;
if (j == m)
return true;
while (i < n && !globMatch(text.substr(i), pattern.substr(j))) {
++i;
}
if (i > 0)
--i;
} else if (pattern[j] == '?' || pattern[j] == text[i]) {
++i;
++j;
} else {
return false;
}
}
while (j < m && pattern[j] == '*') {
++j;
}
return j == m && i == n;
}

GlobMatcher::GlobMatcher(const std::string &patternFile)
{
m_patterns = readGlobPatternsFromFile(patternFile);
}

bool GlobMatcher::match(const std::string &text)
{
for (const auto &pattern : m_patterns) {
if (globMatch(text, pattern)) {
return true;
}
}
return false;
}
17 changes: 17 additions & 0 deletions GlobMatch/globmatcher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <utils/object.hpp>

#include <string>
#include <vector>

class GlobMatcher : noncopyable
{
public:
explicit GlobMatcher(const std::string &patternFile);

bool match(const std::string &text);

private:
std::vector<std::string> m_patterns;
};
23 changes: 23 additions & 0 deletions GlobMatch/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "globmatcher.hpp"

#include <filesystem>
#include <iostream>

auto main() -> int
{
auto path = std::filesystem::current_path().parent_path().parent_path();
std::cout << path << std::endl;
auto filePath = path / ".gitignore";

GlobMatcher globMatcher(filePath.string());
for (const auto &entry : std::filesystem::directory_iterator(path)) {
auto currentPath = entry.path();
if (globMatcher.match(currentPath.filename().string())) {
std::cout << currentPath << " is ignored by .gitignore" << std::endl;
} else {
std::cout << currentPath << " is not ignored by .gitignore" << std::endl;
}
}

return 0;
}
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
14. [MVC](/DesignPattern/MVC/model.hpp)——mvc模式;
15. [Observer](/DesignPattern/Observer/observer.hpp)——观察者模式;
16. [Singleton](/DesignPattern/Singleton/singleton.hpp)——单例模式;
17. [Glog](/Glog/main.cc)——google glog的例子;
18. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装;
19. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印;
20. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现;
21. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子;
22. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
23. [OpenSSL](/OpenSSL)——openssl的一些例子;
17. [GlobMatch](/GlobMatch/globmatcher.hpp)——glob模式匹配的简单实现;
18. [Glog](/Glog/main.cc)——google glog的例子;
19. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装;
20. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印;
21. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现;
22. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子;
23. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
24. [OpenSSL](/OpenSSL)——openssl的一些例子;
1. [aes](/OpenSSL/openssl_aes.cc)——aes加解密的例子;
2. [base64](/OpenSSL/openssl_base64.cc)——base64编解码的例子;
3. [hash](/OpenSSL/openssl_hash.cc)——sha256的例子;
Expand All @@ -41,10 +42,10 @@
7. [sm4](/OpenSSL/openssl_sm4.cc)——sm4加解密的例子;
8. [x509](/OpenSSL/openssl_x509.cc)——x509证书的例子;
9. [bash](/OpenSSL/openssl_bash.sh)——openssl命令行的例子;
24. [Server](/Server)——linux server的一些例子;
25. [Server](/Server)——linux server的一些例子;
1. [server_epoll](/Server/server_epoll.cc)——epoll的例子;
2. [server_poll](/Server/server_poll.cc)——poll的例子;
3. [server_select](/Server/server_select.cc)——select的例子;
25. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
26. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
1. [Thread](/Thread/thread.hpp)——线程类;
2. [ThreadPool](/Thread/threadpool.hpp)——线程池;
8 changes: 4 additions & 4 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"breakpad",
"benchmark",
"gtest",
"crashpad",
{
"name": "openssl",
"features": [
Expand All @@ -23,8 +24,7 @@
"http2",
"tool"
]
},
"crashpad"
}
],
"builtin-baseline": "8150939b69720adc475461978e07c2d2bf5fb76e"
}
"builtin-baseline": "5f4628b89f3f98cd9a0b43c27ded2aa53da1f790"
}

0 comments on commit cd13cdf

Please sign in to comment.