TextMateLib 1.0
Modern C++ implementation of the TextMate syntax highlighting engine
Loading...
Searching...
No Matches
utils.h
1#ifndef TEXTMATELIB_UTILS_H
2#define TEXTMATELIB_UTILS_H
3
4#include "types.h"
5#include <string>
6#include <vector>
7#include <map>
8#include <regex>
9#include <functional>
10
11namespace tml {
12
13// Forward declaration
14struct IOnigCaptureIndex;
15
16// Clone functions
17template<typename T>
18T clone(const T& something);
19
20std::string mergeObjects(const std::string& target, const std::vector<std::string>& sources);
21
22// Basename function
23std::string basename(const std::string& path);
24
25// String comparison functions
26int strcmp_custom(const std::string& a, const std::string& b);
27int strArrCmp(const std::vector<std::string>* a, const std::vector<std::string>* b);
28
29// Hex color validation
30bool isValidHexColor(const std::string& hex);
31
32// Escape regular expression characters
33std::string escapeRegExpCharacters(const std::string& value);
34
35// Returns true if str contains any Unicode character classified as "R" or "AL"
36bool containsRTL(const std::string& str);
37
38// Performance timing
39double performanceNow();
40
41// RegexSource class
42class RegexSource {
43public:
44 std::string source;
45 RuleId ruleId;
46 bool hasAnchor;
47 bool hasBackReferences;
48 std::string anchorCache_A0_G0;
49 std::string anchorCache_A0_G1;
50 std::string anchorCache_A1_G0;
51 std::string anchorCache_A1_G1;
52
53 RegexSource(const std::string& regExpSource, RuleId ruleId_);
54
55 static bool hasCaptures(const std::string* regexSource);
56 static std::string replaceCaptures(const std::string& regexSource,
57 const std::string& captureSource,
58 const std::vector<IOnigCaptureIndex>& captureIndices);
59
60 std::string resolveBackReferences(const std::string& lineText,
61 const std::vector<IOnigCaptureIndex>& captureIndices);
62
63 void buildAnchorCache();
64 std::string resolveAnchors(bool allowA, bool allowG) const;
65
66 RegexSource* clone() const;
67};
68
69// CachedFn template class
70template<typename TKey, typename TValue>
71class CachedFn {
72private:
73 std::map<TKey, TValue> cache;
74 std::function<TValue(const TKey&)> fn;
75
76public:
77 CachedFn(std::function<TValue(const TKey&)> func) : fn(func) {}
78
79 TValue get(const TKey& key) {
80 auto it = cache.find(key);
81 if (it != cache.end()) {
82 return it->second;
83 }
84 TValue value = fn(key);
85 cache[key] = value;
86 return value;
87 }
88};
89
90} // namespace tml
91
92#endif // TEXTMATELIB_UTILS_H
Core type definitions and interfaces for TextMateLib.