Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libgis: Add string concatenation function #1401

Closed
wants to merge 2 commits into from

Conversation

nilason
Copy link
Contributor

@nilason nilason commented Feb 23, 2021

Working on solutions for compiler warnings (#1247), specifically attempting to address a -Wformat-overflow I missed the existence of a string concatenation function.

This PR is a suggestion for a concatenation function added to libgis:

char *G_str_concat(const char **src_strings, int num_strings, const char *sep, int maxsize);

It is based on a local version of the memccpy implementation taken from www.open-std.org : N2349.

The PR consists of two commits, one with the addition of the G_str_concat() function, the other is a fix for the mentioned warning (and serves as usage example).

This function could possibly find its use elsewhere, e.g. r.li.cwed has its own concatenation solution, which may justify the addition.

@nilason nilason added the enhancement New feature or request label Feb 23, 2021
@nilason nilason added this to the 8.0.0 milestone Feb 23, 2021
@nilason
Copy link
Contributor Author

nilason commented Feb 23, 2021

centos7 isn't too happy with this:

strings.c:30:40: error: expected ';', ',' or ')' before 'dst'
 static void *G__memccpy(void *restrict dst, const void *restrict src, int c, size_t n);
                                        ^
strings.c:71:33: error: expected ';', ',' or ')' before 'dst'
 void *G__memccpy(void *restrict dst, const void *restrict src, int c, size_t n)
                                 ^

??
(restrict is a C99 feature...)

@HuidaeCho
Copy link
Member

centos7 isn't too happy with this:

strings.c:30:40: error: expected ';', ',' or ')' before 'dst'
 static void *G__memccpy(void *restrict dst, const void *restrict src, int c, size_t n);
                                        ^
strings.c:71:33: error: expected ';', ',' or ')' before 'dst'
 void *G__memccpy(void *restrict dst, const void *restrict src, int c, size_t n)
                                 ^

??
(restrict is a C99 feature...)

Should we support compiler-specific extensions like __restrict__? Does it mean GCC (or Clang for that matter) does not support C99 (or even C11?) fully or why did they name restrict that way? Just curious.

Reading https://trac.osgeo.org/grass/wiki/RFC/7_LanguageStandardsSupport, have we already reached a consensus about the final language standards?

@nilason
Copy link
Contributor Author

nilason commented Feb 23, 2021

I was trying to please Centos7 CI with __restrict__, as it didn't work with restrict. It uses gcc 4.8.5 and should probably work with -std=c99. Now, this detail (restrict) isn't really a necessity for the function.

lib/gis/strings.c Outdated Show resolved Hide resolved
lib/gis/strings.c Outdated Show resolved Hide resolved
lib/vector/Vlib/dbcolumns.c Outdated Show resolved Hide resolved
lib/vector/Vlib/dbcolumns.c Outdated Show resolved Hide resolved
lib/vector/Vlib/dbcolumns.c Show resolved Hide resolved
lib/vector/Vlib/dbcolumns.c Show resolved Hide resolved
lib/vector/Vlib/dbcolumns.c Show resolved Hide resolved
lib/vector/Vlib/dbcolumns.c Show resolved Hide resolved
include/defs/gis.h Outdated Show resolved Hide resolved
lib/gis/strings.c Outdated Show resolved Hide resolved
@HuidaeCho
Copy link
Member

I was trying to please Centos7 CI with __restrict__, as it didn't work with restrict. It uses gcc 4.8.5 and should probably work with -std=c99. Now, this detail (restrict) isn't really a necessity for the function.

I agree. __restrict__ is not necessary for the function. If anything, it would be a programmer's (our) mistake.

@nilason
Copy link
Contributor Author

nilason commented Feb 23, 2021

I was trying to please Centos7 CI with __restrict__, as it didn't work with restrict. It uses gcc 4.8.5 and should probably work with -std=c99. Now, this detail (restrict) isn't really a necessity for the function.

By default gcc 4.8.5 uses -std=gnu90.

For the record: configuring CentOS7 with -std=gnu99, enables the use of restrict.

@nilason nilason force-pushed the libgis-add-str-concat branch 2 times, most recently from 6620eb1 to 81374ee Compare February 24, 2021 12:23
@nilason
Copy link
Contributor Author

nilason commented Feb 24, 2021

There is memccpy(3) in POSIX. Is there any specific reason for adding our own version of this function?

As I mentioned before, the name memccpy (not the function) has been deprecated with VS 2019 in favour for _memccpy. That leaves us in the future with a deprecation warning (no warnings on CI Windows build just yet) or some preprocessor acrobatics to avoid it. If you have a suggestion on this I'm happy to drop the local implementation of memccpy.

@nilason
Copy link
Contributor Author

nilason commented Feb 24, 2021

@HuidaeCho Thanks ! Applied you suggestions, ready for a new round :) .

@HuidaeCho
Copy link
Member

HuidaeCho commented Feb 24, 2021

@HuidaeCho Thanks ! Applied you suggestions, ready for a new round :) .

@nilason Looks good to me. Just out of curiosity, any reason for double underscores in G__memccpy? The same reason why MS renamed their memccpy to _memccpy? That reasoning may not apply in our code base.

@neteler Do we have a rule for G_name() vs. G__name()? Higher-level vs. lower-level? User developers vs. core developers?

@nilason
Copy link
Contributor Author

nilason commented Feb 24, 2021

@nilason Looks good to me. Just out of curiosity, any reason for double underscores in G__memccpy?

It's just to differentiate against open API.

@HuidaeCho
Copy link
Member

@nilason Looks good to me. Just out of curiosity, any reason for double underscores in G__memccpy?

It's just to differentiate against open API.

Oh! That's a static function.

@HuidaeCho HuidaeCho self-requested a review February 24, 2021 15:20
lib/gis/strings.c Outdated Show resolved Hide resolved
@HuidaeCho
Copy link
Member

@nilason I just have one last suggestion. Please move G__memccpy() and its prototype to the top. The other functions are more for direct string manipulations than memory copy.

lib/gis/strings.c Show resolved Hide resolved
nilason added a commit to nilason/grass that referenced this pull request Feb 24, 2021
nilason added a commit to nilason/grass that referenced this pull request Feb 24, 2021
@nilason
Copy link
Contributor Author

nilason commented Feb 24, 2021

Reordered per suggestion.

lib/gis/strings.c Outdated Show resolved Hide resolved
lib/gis/strings.c Outdated Show resolved Hide resolved
@HuidaeCho
Copy link
Member

Reordered per suggestion.

A separate PR per https://trac.osgeo.org/grass/wiki/Submitting/C#Indentation would be great or maybe together in this PR may be better.

@nilason
Copy link
Contributor Author

nilason commented Feb 24, 2021

I applied the grass_indent.sh, but kept its result only on touched parts (not whole file). That should be for another time/PR.

If I only knew before, there is no need for those 8-sized tabs...

@nilason
Copy link
Contributor Author

nilason commented Feb 24, 2021

Thanks a lot @HuidaeCho !
Cherry picked and pushed manually to keep the commits separate.

@nilason nilason closed this Feb 24, 2021
marisn pushed a commit to marisn/grass that referenced this pull request Mar 22, 2021
marisn pushed a commit to marisn/grass that referenced this pull request Mar 22, 2021
@nilason nilason deleted the libgis-add-str-concat branch April 7, 2021 19:27
ninsbl pushed a commit to ninsbl/grass that referenced this pull request Oct 26, 2022
ninsbl pushed a commit to ninsbl/grass that referenced this pull request Oct 26, 2022
ninsbl pushed a commit to ninsbl/grass that referenced this pull request Feb 17, 2023
ninsbl pushed a commit to ninsbl/grass that referenced this pull request Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants