Author Topic: [solved] Smart Searching and Replacing Text  (Read 7791 times)

0 Members and 1 Guest are viewing this topic.

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
[solved] Smart Searching and Replacing Text
« on: October 29, 2021, 08:41:21 am »
imagine you have a source code written by someone years ago and the names of variables are just a letter

e.g.
Code: [Select]
    uint32_t a;
    uint32_t b;
    uint32_t c;
    uint32_t d;
    ...
    uint32_t m;
    uint32_t s;
    uint32_t t;
    ...
    uint32_t z;

Worse still, the source code is something like 25281 lines of C code with thousands of points where a single letter variable { 'a' .... 'z' } is used, and you are asked to restyle the code with better variable names (plus other code-refactoring, etc etc)

Silly question, but ... how can you search and replace these variable names without doing it manually?  :-//


( I am lost )
« Last Edit: October 30, 2021, 12:17:12 pm by DiTBho »
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #1 on: October 29, 2021, 08:46:29 am »
Code: [Select]
    ...
    a = f(a, b, c, d, m, n, 5, 0xf562);
    d = f(d, a, b, c, m, k, 9, 0xc0b0);
    c = f(c, d, a, b, m, 14, 0, 0x2061);
    b = f(b, c, d, a, 0, 20, 0, 0xe9ba);
    a = f(a, b, c, d, 5, 5, 1, 0xd62d);
    d = f(d, a, b, c, 10, 9, k, 0x0253);
    c = f(c, d, a, b, 15, 14, n, 0xd8a1);
    ...

a single letter also used for function names, f(...) :scared:
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Ed.Kloonk

  • Super Contributor
  • ***
  • Posts: 4000
  • Country: au
  • Cat video aficionado
Re: Smart Searching and Replacing Text
« Reply #2 on: October 29, 2021, 09:03:08 am »
Geez.
iratus parum formica
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 13031
  • Country: us
Re: Smart Searching and Replacing Text
« Reply #3 on: October 29, 2021, 09:06:35 am »
Use a smart code editor, like for example Visual Studio. It will have built-in refactoring tools to allow variable and function renaming, amongst other things.
 
The following users thanked this post: DiTBho

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #4 on: October 29, 2021, 09:17:59 am »
Use a smart code editor, like for example Visual Studio. It will have built-in refactoring tools to allow variable and function renaming, amongst other things.

I usually write code with { Nano, Vim, Geany }. Is there something similar to Visual Studio but open-source?
( not because I hate Microsoft, but because I don't have x86 hardware on hands at the moment, and here I can only run Linux/PPC64)
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 8452
  • Country: ro
Re: Smart Searching and Replacing Text
« Reply #5 on: October 29, 2021, 09:18:24 am »
Find and replace is not the best tool to rename variables.  Try another editor that has rename for variables.

Most of the modern IDEs/editors should have some sort of "Rename Variable" on the right click, or somewhere in the menus, or in the refactoring area.

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #6 on: October 29, 2021, 09:35:26 am »
Find and replace is not the best tool to rename variables.  Try another editor that has rename for variables.

Yup, I have just read the comment on the Microsoft

Quote
If you're renaming code symbols such as variables and methods, it's better to refactor them than to use find-and-replace. Refactoring is intelligent and understands scope, whereas find-and-replace blindly replaces all instances.

I have to find *which* open-source code-editors have this feature  :-//
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 8452
  • Country: ro
Re: Smart Searching and Replacing Text
« Reply #7 on: October 29, 2021, 09:44:48 am »
Is there something similar to Visual Studio but open-source?
( not because I hate Microsoft, but because I don't have x86 hardware on hands at the moment, and here I can only run Linux/PPC64)

Visual Studio is open source for many years now.

It is called 'Visual Studio Code', it's free and open source.
https://linuxize.com/post/how-to-install-visual-studio-code-on-ubuntu-20-04/

I'm not a Microsoft fan, but VS Code is one of the best IDEs ever, IMHO, and it has countless plugins and support for any language.  At some point I was using it instead of Arduino IDE, with programming and serial console, too, from inside VS code.  It was fun to adjust the sources for both the microcontroller part (C++ arduino) and the PC data receiving part (Python) from inside the same IDE.   :D
 
The following users thanked this post: DiTBho

Offline blacksheeplogic

  • Frequent Contributor
  • **
  • Posts: 532
  • Country: nz
Re: Smart Searching and Replacing Text
« Reply #8 on: October 29, 2021, 09:45:02 am »
I have to find *which* open-source code-editors have this feature  :-//

I've not used this but try looking up renamec.vim. I think you need to have built cscope database. YMMV.
 
The following users thanked this post: DiTBho

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: Smart Searching and Replacing Text
« Reply #9 on: October 29, 2021, 10:31:57 pm »
I think Visual Studio Code is the easiest option, and makes most sense here.

You could also use cscope (either from package management, or upstream).  It provides a vim-like editor, and lets you locate and edit each instance; but AFAIK does not provide a batch replacement interface.

If you create file cscope.files containing the (relative) paths to the C source and header files affected, and run cscope -bc, cscope generates an ASCII line-based database cscope.out, that a simple UI could use to make the changes in parallel, showing each change (with a few lines of context) to the user to verify whether the replacement should be applied or not, advancing through the source files in line order; only asking the user whether each change should be applied (y/n/q, for Yes, No, or Quit).  (I would "store" the changes, and not apply them directly, so that the user could also go back to earlier decisions and change them, and only apply the changes after all choices (to all, or just the current file) have been made.  This reduces the risk of bugs due to editing the file and making the database out of date.)
 
The following users thanked this post: DiTBho

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #10 on: October 29, 2021, 11:00:12 pm »
I solved with a library that I developed years ago to  tokenize a source  :D

It understands C-delimiters and line-numbers, so today I wrote an application that accepts "apply from this line of code to this line", compares each token with with a given pattern, and in case of match it renames the token' name but not its properties.

- read the code block range
- tokenize
- within the block, compare token' name and replace
- reassemble and print out

I spent 2 hours on it, it worked insanely great at the first try  :o

I have also looked at this list of IDEs, not yet complied, but CodeLite looks interesting.

Quote
CodeLite is another free and open-source IDE. It supports C/C++, PHP, and JavaScript and is built with the wxWidgets cross-platform toolkit, allowing it to run on Windows, macOS, Linux, and FreeBSD. The editor is based on the open-source Scintilla project.

CodeLite provides a simple but comprehensive user interface and includes support for project management, refactoring, code completion, syntax highlighting, debugging via GNU’s gdb, and integration with source code management systems like Git and Subversion.

The IDE also includes a wxWidgets creation tool that allows developers to design cross-platform graphical user interfaces easily.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Ed.Kloonk

  • Super Contributor
  • ***
  • Posts: 4000
  • Country: au
  • Cat video aficionado
Re: Smart Searching and Replacing Text
« Reply #11 on: October 29, 2021, 11:44:29 pm »
Wow.  :-+
iratus parum formica
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Smart Searching and Replacing Text
« Reply #12 on: October 30, 2021, 04:44:10 am »
I use Code::Blocks, which is quite cognizant of this sort of thing.

May also be some de-obfuscation tools out there, albeit for ad hoc cases... dunno.  JavaScript comes to mind, widely being delivered in compacted, minified, obfuscated formats.  Not that that's necessarily applicable to C syntax, so would likely take a lot of work to adapt.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #13 on: October 30, 2021, 09:46:29 am »
Visual Studio is open source for many years now.

It is called 'Visual Studio Code', it's free and open source.
https://linuxize.com/post/how-to-install-visual-studio-code-on-ubuntu-20-04/


ummm, Microsoft distributes a compiled binary on code dot visualstudio dot com, it's called "Multiplatform Visual Studio Code from Microsoft (binary version)" aka "vscode" (downloadable from here and here ), it has some source to be compiled, but it also looks that the core is released as a binary-blob only available for
  • x86-64 -> VSCode-linux-x64
  • amd64 -> VSCode-linux-x64
  • arm -> VSCode-linux-armhf
  • arm64 -> VSCode-linux-arm64

When they say "multiplatform", they mean that you can download it for { Windows, macOS, Linux } on Visual Studio Code's website, but talking about the supported Linux architectures, well, the bin package only works with { x86/64bit, arm/32bit(hardfloat(1)), arm64 }

Technically it could run on a Allwinner SoC(1)  :o ) , but but unfortunately I'm with a PPC64 machine; I mean ... I could instantiate a Qemu/arm(1) emulator just to run vscode, but well, it doesn't sound a neat solution.

There is also a repository here, and that's what Microsoft says:
Quote
This source code is available to everyone under the standard MIT license.   
And this is about the compiled binary that they distribute on code dot visualstudio dot com
Quote
Visual Studio Code is a distribution of the Code - OSS repository with Microsoft specific customizations released under a traditional Microsoft product license.

That sounds that it's virtually possible but not easy to compile it for my PPC64 machine  :-//
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #14 on: October 30, 2021, 09:50:22 am »
Yup, by Googling I see it's been attempted before, but also it looks like there are still some messy details to work out.

Possible, but not a piece of cake.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #15 on: October 30, 2021, 09:56:19 am »
has anyone tried Anjuta?

it's nice and easy to be compiled, the only problematic dependency is the infamous gnome-libgda library that is still buggy and requires you to configure and build with "introspection" enable otherwise it fails

Quote
*** No rule to make target 'Gda-5.0.typelib', needed by 'all-am'.  Stop.
(nobody knows why, nobody, not even science)
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline PlainName

  • Super Contributor
  • ***
  • Posts: 8774
  • Country: 00
Re: Smart Searching and Replacing Text
« Reply #16 on: October 30, 2021, 10:35:40 am »
Quote
but but unfortunately I'm with a PPC64 machine

Why are you one PPC64? I thought you were developing for that, but assumed you had a 'normal' x86 PC to actually do work on.
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #17 on: October 30, 2021, 11:03:52 am »
Why are you one PPC64?

Long history, mainly used for research.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline nfmax

  • Super Contributor
  • ***
  • Posts: 1684
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #18 on: October 30, 2021, 11:07:34 am »
Can you not edit/refactor your code on an x86 computer, then ship the source over to your PPC for compilation?
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: Smart Searching and Replacing Text
« Reply #19 on: October 30, 2021, 12:16:47 pm »
Can you not edit/refactor your code on an x86 computer, then ship the source over to your PPC for compilation?

I don't have x86 hardware on hands.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17780
  • Country: fr
Re: [solved] Smart Searching and Replacing Text
« Reply #20 on: October 30, 2021, 04:51:17 pm »
As said already, most decent programming editors have that feature.

That said, and probably just like you, I don't even get how anyone could write a 25 kLOC project with such a poor coding style? (And judging from just this variable naming thing, I'm pretty sure this is not the only atrocious side of the coding....) How is it possible to even work on it for more than a couple weeks? ;D
 

Offline PlainName

  • Super Contributor
  • ***
  • Posts: 8774
  • Country: 00
Re: [solved] Smart Searching and Replacing Text
« Reply #21 on: October 30, 2021, 05:09:24 pm »
Quote
I don't even get how anyone could write a 25 kLOC project with such a poor coding style?

Perhaps it wasn't like that originally but got obfuscated deliberately.
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: [solved] Smart Searching and Replacing Text
« Reply #22 on: October 30, 2021, 05:19:02 pm »
Perhaps it wasn't like that originally but got obfuscated deliberately.
You. Probably, or auto-generated by other tools and merged together  :-//
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: [solved] Smart Searching and Replacing Text
« Reply #23 on: October 30, 2021, 05:22:53 pm »
I don't even get how anyone could write a 25 kLOC project with such a poor coding style?

25KLOC processed in 294 seconds by my tool to have decent variable and function names  ;D

2 hours to prepare the tool, 5 months to write the library used by tool, that's computer science  :o :o :o
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5093
  • Country: gb
Re: [solved] Smart Searching and Replacing Text
« Reply #24 on: October 30, 2021, 05:34:16 pm »
There are also 2193 "labels", and 911 "goto"  :o :o :o
This can be fixed by no tool, I can't, you can't, nobody can ...


Certain parts of the source look a dump from tools like "IDA", I wonder is it a kind of reverse engineering from assembly-to-C? Who knows  :-//
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf