Skip to content

Conversation

@jkuebart
Copy link
Collaborator

Only a single PR since several changes have been submitted at once.

I created the »base« branch in order to separate previously reviewed changes. Going forward, it would be useful to retire branches once their PRs and reviews are finished.

jkuebart and others added 4 commits July 23, 2018 16:46
Pass -pthread for compilation and linking as this is a more general way to
build multi-threaded code than just linking to -lpthread.

Fix build of lua and luac (if uncommented) by using identical environments
for all compiler invocations.
Add linker flags to export dynamic symbols. Preprocessor macros and
libraries are taken from Lua's Makefile. Without LUA_USE_DLOPEN, the
following is not supported:

    package.loadlib("abc", "def")

Fix POSIX build of lua and luac by linking against -lm.
src/luautils.h Outdated
#include <string>
#include "lua/src/lua.hpp"

using namespace std;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

»using namespace« in a header file is a no-go: there is no way to undo the effect for its users and it's bound to hide unwanted dependencies between headers: in this case, stringutls.h has errors unless this header is included first because it also uses std:: symbols unprefixed.

Unfortunately, due to the lack of a module system there is no way to avoid namespace prefixes at least in headers.

src/luautils.h Outdated
};

struct StringPtr{
StringPtr(const char* c_, const size_t len_):c(c_),len(len_){}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

std::size_t (also below)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

also, using a different name for c'tor arguments is common, but unnecessary. It is therefore entirely a question of style to write

StringPtr(const char* c, const std::size_t len)
: c(c)
, len(len)
{}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think a constructor taking a std::string would be beneficial for this class:

explicit StringPtr(const std::string& s)
: c(s.data())
, len(s.size())
{}

Copy link
Owner

Choose a reason for hiding this comment

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

Main purpose of this class is to hand c-strings to the overloaded lua_push function as single arguments. As there is also a lua_push that takes std::strings, the StringPtr would not be needed there. Do y... Would there still be a use for the suggested constructor? To make StringPtr more general maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was intended for the call lua_pushfield(L, rule) in main.cpp line 92, but by the time I got there I realised it could just pass the std::string. So you're right, it would actually be confusing to have a std::string c'tor and this struct should stay as it is ;-)

src/luautils.h Outdated

struct StringPtr{
StringPtr(const char* c_, const size_t len_):c(c_),len(len_){}
const char* c;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In order to be consistent with the const member len, this should be

const char* const c;

src/luautils.h Outdated
void lua_push(lua_State *L, const int value){
lua_pushinteger(L, value);
}
void lua_push(lua_State *L, const string value){
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Use std::string and const std::string& for efficiency.

src/luautils.h Outdated
lua_pushinteger(L, value);
}
void lua_push(lua_State *L, const string value){
lua_pushstring(L, value.c_str());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's more efficient to use

lua_pushlstring(L, value.data(), value.size());
  1. Since the length is already known, there is no need for lua_pushstring to walk the string again.
  2. Using .data() instead of .c_str() avoids a potential unnecessary copy in order to add a terminating '\0'
  3. If the string contains embedded '\0' characters, the original version only copies part of the string.

src/main.cpp Outdated
#endif
return result;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

spurious line


#pragma once

#include <iostream>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ostream is sufficient.

const char* s;
size_t num;
};
ostream& operator<< (ostream& stream, const repeat rep){
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

const repeat& is more efficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

use std::ostream

}

// shorten string and remove line breaks and tabs
string shorten(const string& txt, const size_t numchars){
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

use std::string here and below.

print(" ", dump(params))
return params.matched
end

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

two spurious lines before EOF

}

auto lua_loadutils(lua_State *L){
const auto utils = R"(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

indentation looks off

end
end

function Num(params)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This function can go.

@jkuebart
Copy link
Collaborator Author

These commits also contain several whitespace problems, such as indent with spaces in peglib.h, trailing whitespace in main.cpp and peglib.h and trailing newlines in calculator.lua, luautils.h and main.cpp

src/main.cpp Outdated
cerr << "error loading \"" << args[2] << "\": file not found" << endl;
return EXIT_FAILURE;
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

indentation looks off

@mqnc mqnc closed this Jul 30, 2018
@mqnc mqnc deleted the develop branch December 26, 2018 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants