Skip to content

[Refactor] Some more C string removal#8592

Merged
Geod24 merged 7 commits intodlang:masterfrom
Geod24:ddoc-stringification
Aug 24, 2018
Merged

[Refactor] Some more C string removal#8592
Geod24 merged 7 commits intodlang:masterfrom
Geod24:ddoc-stringification

Conversation

@Geod24
Copy link
Member

@Geod24 Geod24 commented Aug 22, 2018

A few more commits cleaning up strings while we wait on #8585 .

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @Geod24! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#8592"

@Geod24 Geod24 force-pushed the ddoc-stringification branch from 802e5c8 to 58ecfc5 Compare August 22, 2018 01:18
src/dmd/doc.d Outdated
}

extern (C++) int icmp(const(char)* stringz, const(void)* s, size_t slen)
private bool icmp(const(char)* stringz, const(char)* s, size_t slen)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t the name a bit misleading now when it will only make a comparison.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely. Any suggestion ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equal, iequal, equali, equalCaseInsensitive.

@Geod24 Geod24 force-pushed the ddoc-stringification branch from 58ecfc5 to c5c7bf9 Compare August 22, 2018 07:10
src/dmd/doc.d Outdated
null
];
for (size_t i = 0; table[i]; i++)
foreach (const entry; table)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The const is redundant, as table is immutable.

src/dmd/doc.d Outdated
extern (C++) int icmp(const(char)* stringz, const(void)* s, size_t slen)
private bool icmp(const(char)* stringz, const(char)* s, size_t slen)
{
size_t len1 = strlen(stringz);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your fixes make all the first arguments to icmp a string, but then they get coerced to pointers by the parameter type, and then strlen is run on them here. Just commit to having string parameters to icmp.

src/dmd/doc.d Outdated
return cast(int)(len1 - slen);
return Port.memicmp(stringz, cast(char*)s, slen);
return false;
return Port.memicmp(stringz, s, slen) == 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give up on pointer-based memicmp and just write a loop.

src/dmd/errors.d Outdated
loc.filename = filename;
loc.linnum = linnum;
loc.charnum = charnum;
Loc loc = Loc(filename, linnum, charnum);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

@Geod24 Geod24 force-pushed the ddoc-stringification branch 2 times, most recently from 0c3e225 to 1f5b35d Compare August 22, 2018 16:34
@Geod24
Copy link
Member Author

Geod24 commented Aug 22, 2018

Updated:

  • Addressed Walter's comment
  • Renamed/refactored icmp into dmd.root.port.Port.iequals and use slicing at call site

Returns:
`true` if `s1 == s2` regardless of case
*/
extern(D) static int iequals(const char[] s1, const char[] s2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Returns int instead of bool
  • Is if worth mentioning in the documentation that this only works with ASCII (as far as I can tell)?

@Geod24 Geod24 force-pushed the ddoc-stringification branch from 1f5b35d to 815d722 Compare August 23, 2018 00:05
@Geod24
Copy link
Member Author

Geod24 commented Aug 23, 2018

Updated according to Jacob's comments:

diff --git a/src/dmd/root/port.d b/src/dmd/root/port.d
index ea52b617b..01ef74d53 100644
--- a/src/dmd/root/port.d
+++ b/src/dmd/root/port.d
@@ -57,6 +57,9 @@ extern (C++) struct Port
     /**
        Compare two slices for equality, in a case-insensitive way

+       Comparison is based on `char` and does not do decoding.
+       As a result, it's only really accurate for plain ASCII strings.
+
        Params:
          s1 = string to compare
          s2 = string to compare
@@ -64,7 +67,7 @@ extern (C++) struct Port
        Returns:
          `true` if `s1 == s2` regardless of case
      */
-    extern(D) static int iequals(const char[] s1, const char[] s2)
+    extern(D) static bool iequals(const(char)[] s1, const(char)[] s2)
     {
         if (s1.length != s2.length)
             return false;

Those constants are not exported to the C++ header,
so there is no point to have them extern(C++) __gshared
when the can just be immutable.
@Geod24 Geod24 force-pushed the ddoc-stringification branch from 815d722 to fc76c62 Compare August 23, 2018 03:23
src/dmd/doc.d Outdated
if (Port.iequals(entry, name[0 .. namelen]))
{
buf.printf("$(DDOC_%s ", table[i]);
buf.printf("$(DDOC_%.*s ", entry.length, entry.ptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using .*s, you must cast the entry.length parameter to int, because entry.length is a size_t.
Also, this change to .*s doesn't buy anything anyway, since the string is guaranteed to be 0 terminated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted this bit to .ptr

Printing is the main issue I encountered when converting pointers to slices, and I think we need a more convenient way to print slices for it to move forward.

@Geod24 Geod24 force-pushed the ddoc-stringification branch from fc76c62 to 4440e8a Compare August 23, 2018 07:41
@Geod24 Geod24 closed this Aug 23, 2018
@Geod24 Geod24 reopened this Aug 23, 2018
Returns:
`true` if `s1 == s2` regardless of case
*/
extern(D) static bool iequals(const(char)[] s1, const(char)[] s2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Port was originally intended to encapsulate platform-specific code, hence the name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit surprised too, but decided to go with consistency.
Moved iequals to utils

@Geod24 Geod24 force-pushed the ddoc-stringification branch from 4440e8a to 83f93d3 Compare August 24, 2018 01:02
if (!writeToEnv(environment, strdup(pn)))
{
error(Loc(filename, lineNum, 0), "Use `NAME=value` syntax, not `%s`", pn);
error(filename, lineNum, 0, "Use `NAME=value` syntax, not `%s`", pn);
Copy link
Contributor

@dnadlinger dnadlinger Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is imho a regression in legibility/code quality. If you want to eliminate an overload, I'd prefer getting rid of the non-Loc one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I only considered the fact that this overload was used in only one place.
However changing this is a bit more involved and would trigger other changes, so I simply removed this commit from this PR and will do the changes in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Geod24 Geod24 force-pushed the ddoc-stringification branch from 83f93d3 to 3bf2778 Compare August 24, 2018 04:25
case ' ':
case '\t':
case '\n':
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is personal taste, but I found the continue version easier to read, as it can't be confused with breaking out of the loop.

{
return hints.get(cast(string) s[0..strlen(s)], null).ptr;
if (auto ptr = s.toDString() in hints)
return (*ptr).ptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if anything containing the snippet (*ptr).ptr can qualify as an improvement…

src/dmd/link.d Outdated
version (OSX)
{
__gshared const(char)* nmeErrorMessage = "`__Dmain`, referenced from:";
static immutable nmeErrorMessage = "`__Dmain`, referenced from:";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho a raw pointer is more appropriate as long as it's only used as a C string as it encodes the presence of a null character by convention. Again just a question of style, though.

@dnadlinger
Copy link
Contributor

@Geod24: Feel free to auto-merge if you don't want to spend any time discussing stylistics…

Geod24 added 6 commits August 24, 2018 14:08
This was previously used in doc, however as we move more towards
slice, the code could be simplified to the point it had to be removed.
Newly added `dmd.utils : iequals` will perform a case-insensitive string
comparison of two slices.
@Geod24 Geod24 force-pushed the ddoc-stringification branch from 3bf2778 to f55173e Compare August 24, 2018 05:09
@Geod24
Copy link
Member Author

Geod24 commented Aug 24, 2018

Updated:

diff --git a/src/dmd/imphint.d b/src/dmd/imphint.d
index 95f6319f0..57631750a 100644
--- a/src/dmd/imphint.d
+++ b/src/dmd/imphint.d
@@ -23,8 +23,8 @@ import dmd.utils;
  */
 extern (C++) const(char)* importHint(const(char)* s)
 {
-    if (auto ptr = s.toDString() in hints)
-        return (*ptr).ptr;
+    if (auto entry = s.toDString() in hints)
+        return entry.ptr;
     return null;
 }
diff --git a/src/dmd/doc.d b/src/dmd/doc.d
index 60b9b724d..44fc81782 100644
--- a/src/dmd/doc.d
+++ b/src/dmd/doc.d
@@ -1846,7 +1846,7 @@ extern (D) const(char)[] skipwhitespace(const(char)[] p)
         case ' ':
         case '\t':
         case '\n':
-            break;
+            continue;
         default:
             return p[idx .. $];
         }
diff --git a/src/dmd/link.d b/src/dmd/link.d
index 3334d2e0a..2d67e08e0 100644
--- a/src/dmd/link.d
+++ b/src/dmd/link.d
@@ -112,11 +112,11 @@ version (Posix)
     {
         version (OSX)
         {
-            static immutable nmeErrorMessage = "`__Dmain`, referenced from:";
+            static immutable(char*) nmeErrorMessage = "`__Dmain`, referenced from:";
         }
         else
         {
-            static immutable nmeErrorMessage = "undefined reference to `_Dmain`";
+            static immutable(char*) nmeErrorMessage = "undefined reference to `_Dmain`";
         }
         FILE* stream = fdopen(fd, "r");
         if (stream is null)
@@ -136,7 +136,7 @@ version (Posix)
             const(char)* lastSep = strrchr(buffer.ptr, '\n');
             if (lastSep)
                 buffer[(end = lastSep - &buffer[0])] = '\0';
-            if (strstr(&buffer[0], nmeErrorMessage.ptr))
+            if (strstr(&buffer[0], nmeErrorMessage))
                 nmeFound = true;
             if (lastSep)
                 buffer[end++] = '\n';

@Geod24
Copy link
Member Author

Geod24 commented Aug 24, 2018

Well this is green with auto-merge but dlang bot doesn't care, so merging manually

@Geod24 Geod24 merged commit 6a1e255 into dlang:master Aug 24, 2018
@Geod24 Geod24 deleted the ddoc-stringification branch August 24, 2018 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants