🛰️航天仿真算法库 SpaceAST 0.3.1
载入中...
搜索中...
未找到
SourceLocation.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24
25AST_NAMESPACE_BEGIN
26
34constexpr bool is_sep(char c) {
35#ifdef _WIN32
36 return c == '\\' || c == '/';
37#else
38 return c == '/';
39#endif
40}
41
42
43constexpr const char* extract_after_last_dotdot_impl(const char* str,
44 const char* last,
45 const char* orig)
46{
47 return *str == '\0'
48 ? (last ? last : orig) // 扫描结束:有匹配则返回匹配后位置,否则返回原串
49 : (str[0] == '.' && str[1] == '.' && is_sep(str[2]))
50 ? extract_after_last_dotdot_impl(str + 3, str + 3, orig) // 匹配到 ..\,更新 last 并继续
51 : extract_after_last_dotdot_impl(str + 1, last, orig); // 未匹配,继续扫描
52}
53
54
56constexpr const char* extract_after_last_dotdot(const char* str)
57{
58 return extract_after_last_dotdot_impl(str, nullptr, str);
59}
60
61
62constexpr const char* extract_filename_impl(const char* str, const char* last, const char* orig)
63{
64 return *str == '\0'
65 ? (last ? last + 1 : orig) // 返回最后一个分隔符后的位置,若无则返回原串
66 : (is_sep(*str)
67 ? extract_filename_impl(str + 1, str, orig) // 更新最后一个分隔符位置
68 : extract_filename_impl(str + 1, last, orig));
69}
70
71
73constexpr const char* extract_filename(const char* str, const char* last_sep_pos=nullptr)
74{
75 return extract_filename_impl(str, nullptr, str);
76}
77
78
79
80#define A_SOURCE_FILE_PATH _AST extract_after_last_dotdot(__FILE__) // 仅包含 .. 后的路径部分
81#define A_SOURCE_FILE_NAME _AST extract_filename(__FILE__)
82#define A_SOURCE_LINE __LINE__
83#define A_SOURCE_FUNC __FUNCTION__
84
87AST_NAMESPACE_END
constexpr bool is_sep(char c)
判断字符是否为路径分隔符(反斜杠或正斜杠)
定义 SourceLocation.hpp:34
constexpr const char * extract_filename(const char *str, const char *last_sep_pos=nullptr)
从路径中提取文件名,若无分隔符则返回原串
定义 SourceLocation.hpp:73
constexpr const char * extract_after_last_dotdot(const char *str)
查找最后一个 ".." 后的路径,并返回后续实际路径的起始位置,若无 .. 则返回原串
定义 SourceLocation.hpp:56