comparison libinterp/corefcn/utils.h @ 29091:b924b916dc91

matlab.lang.makeValidName: reimplement in C++ (patch #9998) Rationale: `matlab.lang.makeValidName` was pure Octave code and only available from the interpreter via `octave::feval`. This is too slow for an Octave function ensuring a valid variable name, especially, when called many times from C++. For example in jsondecode. * libinterp/corefcn/utils.h: new function `octave::make_valid_name` and new helper class `octave::make_valid_name_options`. * libinterp/corefcn/utils.cc: new functions `F__make_valid_name__` and `octave::make_valid_name` and new helper class `octave::make_valid_name_options`. * scripts/+matlab/+lang/makeValidName.m: replace Octave code implementation by `F__make_valid_name__` call.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Mon, 23 Nov 2020 16:11:46 +0900
parents 376ca9022b7e
children 7854d5752dd2
comparison
equal deleted inserted replaced
29090:f61d1faacfca 29091:b924b916dc91
43 43
44 namespace octave 44 namespace octave
45 { 45 {
46 extern OCTINTERP_API bool valid_identifier (const char *s); 46 extern OCTINTERP_API bool valid_identifier (const char *s);
47 extern OCTINTERP_API bool valid_identifier (const std::string& s); 47 extern OCTINTERP_API bool valid_identifier (const std::string& s);
48
49 //! Helper class for `make_valid_name` function calls.
50 //!
51 //! Extracting options separately for multiple (e.g. 1000+) function calls
52 //! avoids expensive repetitive parsing of the very same options.
53
54 class
55 OCTINTERP_API
56 make_valid_name_options
57 {
58 public:
59
60 //! Default options for `make_valid_name` function calls.
61 //!
62 //! Calling the constructor without arguments is equivalent to:
63 //!
64 //! @code{.cc}
65 //! make_valid_name_options (ovl ("ReplacementStyle", "underscore",
66 //! "Prefix", "x"));
67 //! @endcode
68
69 make_valid_name_options () = default;
70
71 //! Extract attribute-value-pairs from an octave_value_list of strings.
72 //!
73 //! If attributes occur multiple times, the rightmost pair is chosen.
74 //!
75 //! @code{.cc}
76 //! make_valid_name_options (ovl ("ReplacementStyle", "hex", ...));
77 //! @endcode
78
79 make_valid_name_options (const octave_value_list& args);
80
81 //! @return ReplacementStyle, see `help matlab.lang.makeValidName`.
82
83 const std::string&
84 get_replacement_style () const { return m_replacement_style; }
85
86 //! @return Prefix, see `help matlab.lang.makeValidName`.
87
88 const std::string& get_prefix () const { return m_prefix; }
89
90 private:
91
92 std::string m_replacement_style{"underscore"};
93 std::string m_prefix{"x"};
94 };
95
96 //! Modify @p str to be a valid variable name.
97 //!
98 //! @param str input string
99 //! @param options see also `help matlab.lang.makeValidName`.
100 //!
101 //! @return true, if @p str was modified.
102
103 extern OCTINTERP_API bool
104 make_valid_name (std::string& str, const make_valid_name_options& options);
48 105
49 extern OCTINTERP_API bool 106 extern OCTINTERP_API bool
50 same_file (const std::string& f, const std::string& g); 107 same_file (const std::string& f, const std::string& g);
51 108
52 extern OCTINTERP_API int almost_match (const std::string& std, 109 extern OCTINTERP_API int almost_match (const std::string& std,