From 68a92f95bcda4fc76b7ed2ee9795d0fdcd3d9f6d Mon Sep 17 00:00:00 2001 From: taynpg Date: Fri, 10 Jan 2025 14:06:58 +0800 Subject: [PATCH] =?UTF-8?q?compile=EF=BC=9A=E7=BB=86=E8=8A=82=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 34 ++++++++++++++++++++++++++++++++++ filecomplete.cpp | 34 +++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bff8a15 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +# 编译器和编译选项 +CXX = g++ +CXXFLAGS = -Wall -Wextra -std=c++17 -O2 + +# 文件和目标 +LIB_SRC = filecomplete.cpp +LIB_OBJ = $(LIB_SRC:.cpp=.o) +LIB_HEADER = filecomplete.h + +MAIN_SRC = main.cpp +MAIN_OBJ = $(MAIN_SRC:.cpp=.o) +TARGET = main + +# 默认目标 +all: $(TARGET) + +# 生成目标文件 +$(TARGET): $(LIB_OBJ) $(MAIN_OBJ) + $(CXX) $(CXXFLAGS) -o $@ $^ + +# 编译库源文件 +$(LIB_OBJ): $(LIB_SRC) $(LIB_HEADER) + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# 编译主程序文件 +$(MAIN_OBJ): $(MAIN_SRC) + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# 清理生成文件 +clean: + rm -f $(LIB_OBJ) $(MAIN_OBJ) $(TARGET) + +# 伪目标 +.PHONY: all clean diff --git a/filecomplete.cpp b/filecomplete.cpp index 0f33694..8dc5d02 100644 --- a/filecomplete.cpp +++ b/filecomplete.cpp @@ -83,8 +83,6 @@ static std::vector> buf{}; static std::vector word{}; static size_t wo{}; static size_t len{}; -static short cy{}; -static short cx{}; static char* main_buf{}; void trans2buf(char* buffer); @@ -210,9 +208,9 @@ std::pair get_wh() #endif } -int get_u8_len(unsigned char ch) +size_t get_u8_len(unsigned char ch) { - if (ch >= 0x00 && ch <= 0x7F) { + if (ch <= 0x7F) { return 1; } else if ((ch & 0xE0) == 0xC0) { return 2; @@ -225,8 +223,10 @@ int get_u8_len(unsigned char ch) } else if ((ch & 0xFE) == 0xFC) { return 6; } else { - return 0; + printf("invalid u8 first ch."); + exit(1); } + return 0; } void supply(std::vector& wch, char ch) @@ -436,13 +436,13 @@ char* fc_readline() color_print(str_predict.data(), DEFAULT_PREDICT_COLOR); } // Move cursor - int cur_pos{}; + size_t cur_pos{}; #if defined(STRCODE_GBK) for (int i = 0; i < buf.size() && i < wo; ++i) { - cur_pos += static_cast(buf[i].size()); + cur_pos += buf[i].size(); } #else - for (int i = 0; i < buf.size() && i < wo; ++i) { + for (size_t i = 0; i < buf.size() && i < wo; ++i) { if (buf[i].size() > 1) { cur_pos += 2; } else { @@ -477,20 +477,20 @@ char* fc_readline() // 在这里补全 if (!str_predict.empty()) { std::vector> temp; - for (int i = 0; i < str_predict.size();) { + for (size_t i = 0; i < str_predict.size();) { std::vector b; char curch = str_predict[i]; b.push_back(curch); - if (curch >= 0 && curch < 128) { + if (curch >= 0) { temp.push_back(b); ++i; } else { #if defined(STRCODE_GBK) - int length = 2; + size_t length = 2; #else - int length = get_u8_len(curch); + size_t length = get_u8_len(curch); #endif - for (int z = 1; z < length; ++z) { + for (size_t z = 1; z < length; ++z) { if ((i + z) < str_predict.size()) { b.push_back(str_predict[i + z]); } @@ -521,10 +521,14 @@ char* fc_readline() #endif switch (_getch()) { case LEFT: - wo = wo < 1 ? 0 : (--wo); + if (wo > 0) { + --wo; + } break; case RIGHT: - wo = wo >= len ? len : (++wo); + if (wo < len) { + ++wo; + } break; case UP: break;