C++ 11 compatible regex from .NET compatible regex - regex

I have the following regex that works with the .NET regex engine (FYI, what this does is to parse command line arguments of the form name="value1,value2" name2 = value3 where the quotes are optional)
(?<name>[^=]+)=?((?<quoted>\""?)(?<value>(?(quoted)[^\""]+|[^,]+))\""?,?)
and want to use this in C++ 11. However, I understand that there are no named groups or conditions(? not entirely sure) in C++'s regex syntax.
I'm asking this question here because I haven't found any tools/web pages where I can easily test a regex and have it work in C++ 11 (whereas there are tools for almost all other platforms). A link to an online (or offline) C++ 11 compatible regex tester tool that can show me groups, subgroups and display helpful error messages would also be an acceptable answer.
PS: I understand that command line parsing is an exercise best suited to a parser but I have been using this simple regex long enough without any issues for most of my command-line tools that I am ok with any drawbacks it may have.

The support for regex in C++11 is still sketchy, for example gcc will have good regex support only in version 4.9. You haven't specified what's your environment, but you can find details about what's supported and what's not in this question.
Your best bet is using the boost regex library - you should read Understanding Marked Sub-Expressions and Captures.
To make it a bit easier for you to test and experiment, as you requested, here's a ready environment that adapts the example from the boost article, with the right compilation flags g++-4.8 -O2 -Wall -pedantic -pthread -DBOOST_REGEX_MATCH_EXTRA main.cpp -lboost_regex set up: http://coliru.stacked-crooked.com/a/8950eb9c097b0db1

Related

How do I ag search into a specific set of folders using the -G option?

How do I ag search into a specific set of folders using the -G option?
Here's an example where I use -G routes, but it's picking up another result from another folder, because routes is still in the path. Yet if I try -G ^routes, it doesn't seem like the regex is taking?
On that note, Atom has a nice path searching syntax which lets me do things like app,routes,!storage (search in app and routes folders, but ignore storage folder). Since I've switched to vim, I'm finding it hard to get a searching workflow down with ack/ag. Anyone have any tips for me?
I suppose you'd really like to try this plugin https://github.com/junegunn/fzf.vim. It will (among other things) run your ag search and present the result in a fuzzyfinder. Then you'll have the very same feature you mention in atom by running :Ag middleware followed by(in the fzf window) 'app/ 'routes/ !storage/'.
about the use of regex for the file pattern, you'll need to quote your regex
ag -G '^routes' middleware
that being said, the '^...' doesn't work as intended here on my computer either, although '\b' does. I started using ripgrep instead of the-silver-searcher not so much because it's faster but rather because it has a better documentation, maybe you'd like to give it a try. rg -g will take a glob which is easier to understand than the "FILEPATTERN" mentioned in the ag man page

set Vim as a basic C++ Editor [duplicate]

This question already has answers here:
Configuring Vim for C++
(3 answers)
Closed 9 years ago.
I want to set Vim to work with C++, I just want to perform these tasks:
write code (you don't say?)
check and highlight C++ syntaxis
autocompletion (if is possible)
compile, run, debugging and return to the editor
tree-view project files on the side
statusbar
I know that much of this tasks can be done with plugins, so I need your help to make a list of required plugins and how to set them up together.
why basic? well, I'm taking the programming course level 1 in my university, and we will make simple command-line programs, simple such a mathematical evaluations (functions, array even or odd numbers, draw triangles with asterisks and so.)
I don't think you need any plugins... the features you want are already there.
-write code (you don't say?)
this is a given
-check and highlight C++ syntax
:syntax enable
-autocompletion (if is possible)
in insert mode, try
ctrl-n
ctrl-p
-compile, run, debugging and return to the editor
vim is an editor, not a complier. You can, however, drop into a shell to run these commands or use :!commandname. Try one of the following
ctrl-z
g++ -o myprogram myprogram.cpp
fg
or
:!g++ -o myprogram myprogram.cpp
or just keep another terminal open.
-tree-view project files on the side
:!tree -C | less -R
-statusbar
already at the bottom. Try gvim for more toolbars et cetra.
Have fun!
BTW - this message was brought to you via vim and pentadactyl
Some plugins that might help you and I tried in the past when I was trying to get started with vim long ago:
IDE: http://www.vim.org/scripts/script.php?script_id=213
Tree view: http://www.vim.org/scripts/script.php?script_id=1658
Debugging: http://www.vim.org/scripts/script.php?script_id=3039
Completion: http://ctags.sourceforge.net/ and http://www.vim.org/scripts/script.php?script_id=1520
Statusbar: http://www.vim.org/scripts/script.php?script_id=3881 and its successor http://usevim.com/2013/01/23/vim-powerline/
You can search for further plugins at http://www.vim.org/scripts/index.php
That being said, I use vim just fine without any plugin for daily C++ development. It is also handy because I can use the same workflow when ssh'ing onto a server or someone else's machine without the consideration of major differences.
Also C++ syntax highlight works by default as such plugins for languages are usually included into the distributed vim, already.

Google c style settings for gnu indent?

I am using google c indent style for Emacs (google-c-style.el) and Vim(google.vim).
But since I have some existing code that is not this style and I hope I can change it. I find there is a tool called GNU indent that can do such thing automatically and it provides some common style settings on this page, however there is no for Google c indent style. SO is there equivalent for it as well?
(I tried the Linux and Berkley style and feel that they are by no means satisfactory for me)
For the record, there is an alternate solution for those who are interested in Clang and LLVM.
clang-format definitely can help format existing source code easily and efficiently. It has explicit builtin support for 5 format, namely LLVM(default), Google, Chromium, Mozilla, WebKit.
The simples way to format a file with Google style is:
clang-format -style=Google -i filename
Where -i means inplace modification, you may try without this option to preview the changes.
To batch format existing C/C++ code we can simply use the command like:
find . -name "*.cc" | xargs clang-format -style=Google -i
Apart from the listed 5 formats, there are actually other styles like GNU(added on revision 197138; it's a pity that the document is not synced).
Note that clang-format accepts rc like files named .clang-format or _clang-format in a project, the simplest way to add such a configuration file(as said in clang-format's official tutorial page) is to dump the configuration of an existing format like:
clang-format -style=Google -dump-config >.clang-format
Also you might also use BasedOnStyle option so a configuration file might look like:
---
BasedOnStyle: Chromium
PointerBindsToType: false
ObjCSpaceAfterProperty: true
...
Use .clang-format or _clang-format as keywords to search on Github and there are other samples; or you might refer to this site to help build one.
There are also integrations for IDEs/Editors such as Visual Studio(in directory clang-format-vs), Sublime, Emacs, Vim(all in directory clang-format).
Another 3 tips:
For Emacs integration(clang-format.el), personally I think it's better to bind key for clang-format-buffer rather than clang-format-region.
For Mac OSX homebrew installation, use brew install --with-clang, --with-lld, --with-python --HEAD llvm can get clang-format support and its integration files are in $(brew --cache)/llvm--clang--svn-HEAD/tools/clang-format(bonus: there is even a git-clang-format there!!).
There are other fabulous tools inside clang-extra-tools such as clang-modernize(which is used to "automatically convert C++ code written against old standards to use features of the newest C++ standard where appropriate"), really worthy of having a try!
A brief reading of the google coding style shows that it is mostly K&R coding style, except with 2 space indentation (including case statements), 80 column lines, and no tabs. So, the following options should accomplish that:
-kr -ci2 -cli2 -i2 -l80 -nut
Start with that. You may have to tweak the resulting code. C++ support, in particular, is weak for indent.
Legend:
-kr: K&R style
-ci2: Continuation indent, the lines following the first line of a multi-line code statement get indented by 2 spaces
-cli2: Case label indent, case labels are indented 2 spaces in from the switch
-i2: Indentation, 2 spaces
-l80: Length, 80 columns
-nut: No tabs
As an alternative, you may consider executing emacs in batch mode to apply indentation on your code for you. Briefly:
Create a file called emacs-format-file with the contents:
(defun emacs-format-function ()
"Format the whole buffer."
(c-set-style "Google")
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max))
(save-buffer))
Execute the following command from the shell:
emacs -batch your_source_file.c \
-l emacs-format-file -f emacs-format-function

Setting up vim as C++ IDE

I wish to setup vim as C++ IDE so I can do all work from it.
I'm using these plugins for vim:
Clang complete - accurate completion
nerdtree - browse files
snipmate - insert snippets
AutoComplPop - omni-completion
buffergator - buffer management
vim-powerline - nice statusbar
vundle - to manage plugins
But I lack things like Jump to definition and compiling multiple files in one executable, project view...
I'm using
nmap <F8> :w % <bar> :!g++ -W -Wall -Wextra -pedantic -std=c++11 % -o %:t:r<CR> <bar> :!./%:t:r<CR>
to compile current file, but it won't work if there are multiple file that create one executable.
I know I could just use eclipse, netbeans, code::blocks and such, but I really like vim... If such thing as vim ide isn't possible do I have to learn GNU build system or some other method?
Any advice is welcome.
You need to create a makefile which handles the build process.
Then from vim just run :make, it will run the build and pop all errors in quickfix window where you can navigate and jump to error locations.
First, to jump to definitions, you might try this:
http://www.santiagolizardo.com/article/vim-jump-to-classes-and-functions-defined-in-different-files/64003
I haven't tested it, so I can't tell you if it works.
Now, to build multiple file projects, it might be better for you to learn how to use makefiles and automake. These links might help you:
http://homepages.gac.edu/~mc38/2001J/documentation/g++.html
http://www.openismus.com/documents/linux/automake/automake
Good luck.
Edit: A similar question was answered on this link: https://stackoverflow.com/a/563992/1820837
"Jump to definition" is already there, it's <C-]> with the cursor on a keyword or :tag foo on the command line.
For these to work, you need a tags file generated by exuberant-ctags and to tell Vim where to find it. See :help tags and :help ctags.
Without a tags file, gd goes to the definition of the keyword under your cursor if it's in the same file. But it's not as generally useful as <C-]>.
For "Jump to definition" I can recommend the YouCompleteMe, plugin which is really easy to setup with vundle.
Otherwise there is also ctags, but I find it less useful.
To use vim as a IDE, I find this post useful.

Using Eclipse CDT from command line

I need to have some of my C++ classes, functions and namespaces renamed as a part of my build script, which is runned by my CI system.
Unfortunatly a simple sad/awk/gsar/... technique is not enough, and I need smart rename refactoring, that carefully analyses my code.
Actually I found out, that CDT C/C++ rename refactoring does, what I need. But it does it from Eclipse IDE. So I need to find a way to start it from command line, and to make it a part of my CI build script.
I know that Eclipse has eclipsec executable, that allowes running some Eclipse functions from command line (see e.g. here).
But I can't find any suitable documentation for functions, CDT exports to command line. The only thing, I found is the this. But it doesn't solve my problem.
So, I need help to run CDT rename refactoring from command line (or someway like that). If it is not possible, may be someone will advice another tool, that can do rename refactoring for C++ from command line ?
Pragmatic Approach
"I need to have renamed as a part of my build script"
This sounds a bit like a design problem. However, I remember having been guilty of the same sin once writing a C++ application on AIX/Win32: most notably, I wanted to be able to link 'conflicting' versions of shared objects. I solved it using a simple preprocessor hack like this:
# makefile
#if($(ALTERNATIVE))
CPPFLAGS+=-DLIBNAMESPACE=MYLIB_ALTERNATIVE
#else
CPPFLAGS+=-DLIBNAMESPACE=MYLIB
#endif
./obj64/%.o: %cpp
xlC++ $(CPPFLAGS) $^ -o %#
Sample source/header file:
namespace MYLIB
{
class LibService :
{
};
}
As you can see, this required only a single
find -iname '*.[hc]pp' -o -iname '*.[hc]' -print0 |
xargs -0 sed -i 's/OldNamespace/MYLIB/g'
Eclipse Automation
You could have a look at eclim, which does most, if not all, of what you describe, however it targets the vim editor.
What eclim boasts, is full eclipse intergration (completion, refactoring, usage search etc.) from an external program. I'm not fully up to speed with the backend of eclim, but I do know that it works with a eclimd server process that exposes the service interface used by the vim plugin.
I suspect you should be able to reuse the code from eclimd if not just use eclim for your purposes.
We are completing a command-line rename tool for C++, that uses compiler accurate parsing and name resolution, including handling of shadowed names. Contact me (see bio) for further details or if you might be interested in a beta.