regular expression to rename pointer variables - regex

I have this much
Code snippet:
int *filePointer;
float *valPtr;
*valPtr = 5.6;
filePointer = ∑
I would like to replace all pointer variables as follows, for example
*filePointer have to be converted to *filePointer_p.
filePointer have to be converted to filePointer.
valPtr have to be converted to valPtr_p
How can I do it using Regular Expression.

It sounds to me like you might be asking how to use regular expressions as a generic tool to rename all pointer variables in source code. I am assuming that the provided snippet is just an example.
If my understanding of your goal is correct, it is not possible to do that with regular expressions. A regular expression would not be able to determine reliably from the context if a variable is a pointer. Consider, for example, a=b;. a and b could be pointers or they could be most anything else. A regular expression by itself would not be able to determine that.

The first is simply:
s/\*filePointer/*filePointer_p/g
Not sure what you're second is on about? But if you're basically out to rename filePointer, then you should be able to simply do:
s/filePointer/filePointer_p/g
on the assumption that there aren't any other variables or context that contain the text filePointer in a different context.

For 1, try s/\*filePointer/*filePointer_p/g.

Related

Placeholder variable type

I need to get information from a class function. Said class has overloaded operators for basically any standard type. Therefore
double foo = exampleObject.getInformation();
and
std::string faa = exampleObject.getInformation();
Would both work. If the information can not be transformed into a double, foo will be set to 0. The initialization of faa will always work. (It can always be expressed as a string)
My problem is: I want to get the information and save it as a double variable, if that can not be done as the information is not of numeric type, I want the variable to be a string. I basically need a variable that can change its type. How do I do this? I'm sorry if that is a very basic question, C++ is not my main programming language.
Have you tried using Function Templates?
They won't change the type of a variable but will allow you to write your code in a way that works with more than 1 data type.
If c++ is not your main, I would recommend checking the checking the documentation for Function Templates on cplusplus.com
Here => https://cplusplus.com/doc/oldtutorial/templates/

How can I construct an array of static regexes in D?

I have several regular expressions that need to be applied in a row. For example:
import std.regex;
auto ctrex1 = ctRegex!(`def\s\n`);
auto ctrex2 = ctRegex!(`func\(`);
I'd like to wrap them in an array and use it as a function argument.
StaticRegex!char[] staticRegexes = [ctrex1, ctrex2];
It does not work however and the compiler throws
Error: static variable ctrex1 cannot be read at compile time
My naive understanding is that after compiler reads and compiles ctrex1 and ctrex2 there should not be a problem for it to compile an array for these objects later below. Assuming it just reads the statements from top to bottom. But it looks like my understanding is wrong. How does compiler work in this case and how do I construct an array of static regexes?
You can't have an array of compile-time regular expression objects, because an array's values all must have the same type, but a compile-time regular expression has the expression string as a part of its type.
You could instead have a tuple of compile time regular expression objects, or add a level of indirection and wrap each into an uniform delegate, which then invokes the compile time regular expression object's matcher.

having regex object is it possible to recreate the string pattern back from it? [duplicate]

Can I get the string with regular expression from std::regex? Or should I save it somewhere else if I want to use it later?
In boost you can do this:
boost::regex reg("pattern");
string p = reg.str();
or use << operator
cout << reg; will print pattern.
but in std::regex there is no str() or operator<<. Should I save my string somewhere else or I just can't find it?
In debugger I can see what's in std::regex.
I just looked in N3225, section 28.4 (header <regex> synopsis) and indeed, the basic_regex template has no member function str, and there are no operator<< provided.
The paragraph 28.8/2 provides a little insight on this :
Objects of type specialization of
basic_regex are responsible for
converting the sequence of charT
objects to an internal representation.
It is not specified what form this
representation takes, nor how it is
accessed by algorithms that operate on
regular expressions.
What I understand is that the standard mandates that basic_regex can be constructed from const charT * but does not require the implementation to keep this string.
The MSDN docs seem to show that there's no publicly accessible way to retrieve the regex pattern from a constructed object, so I would say that you need to save the string yourself.

Boost phoenix value and function return value type

I'm very new to Boost::Phoenix and I'm using it to do FP in C++. I went though the tutorial on their official pages. However, I'm wondering why no examples show how to "Save" the variables. For example, in the values example it says to use a function variable
std::cout << val(3)() << std::endl;
it directly prints out the executed result. What if I want to save the variable? like
type t = val(3);
What is the type of val(3)? Same thing happens when I want to declare the type of a function variable returned by a lazy function. What is the type of it? I don't understand why the entire tutorial always output it immediately. Am I using it wrong?
Thanks,
Yi
You can always (demangle) typeid(...phoneix expression...).name() (or create a compiler error) to see the type of the expression. Soon you will realize that you are not meant (and is not practical) to know the type that represents the expression (they are tens of lines long in some cases).
So answering your first question:
typeid(boost::phoenix::val(3.) =
boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<double>, 0l> >
Everything inside actor is an implementation detail that you should not rely on.
In C++11 you can use auto but since all you want to know is the function aspect of it you can do almost achieve the same thing by storing the expression as boost::function (now std::function). For example:
auto f1 = boost::phoenix::val(3.);
std::function<double()> f2 = boost::phoenix::val(3.);
Then
f1()
gives 3.
f2()
also gives 3.
Answering your second question, if you need to know the type of the expression your are using the library in the wrong way in my opinion, because that is an implementation detail (in fact it changed in different version of Phoenix).

Reusing a character class in a regular expression

In order to keep a regular expression more brief, is there a shorthand way to refer to a character class that occurs earlier in the same regular expression?
Example
Is there a way to shorten the following:
[acegikmoqstz##&].*[acegikmoqstz##&].*[acegikmoqstz##&]
Keep in mind that regex features are dependant on the language being used.
With Java, you can do this:
[acegikmoqstz##&](?:.*[acegikmoqstz##&]){2}
But that's all, with java you can't refer to named subpattern.
With PHP you can do that:
(?(DEFINE)(?<a>[acegikmoqstz##&]))\g<a>(?:.*\g<a>){2}