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

Adding support for std::string_view to sf::String #2451

Closed
wants to merge 6 commits into from

Conversation

khatharr
Copy link

Description

Revision of #2447

I went ahead and cleaned up formatting and added comments, just to get it done.

This PR is related to the issue #2445

Tasks

  • Tested on Linux
  • Tested on Windows
  • Tested on macOS
  • Tested on iOS
  • Tested on Android

How to test this PR?

Test driver (same as before):

#include <SFML/Main.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include <string_view>

int main() {
  sf::RenderWindow window(sf::VideoMode(sf::Vector2u(800, 600)), "SFML Testbed");
  window.setFramerateLimit(60);

  sf::Font font;
  bool derp = font.loadFromFile("c:/Windows/Fonts/arial.ttf");
  
  const char data[] = "derpdurr";
  const wchar_t wdata[] = L"herpboing";

  std::string str(data + 4, 4);
  std::string_view sv(data, 4);
  std::wstring wstr(wdata, 4);
  std::wstring_view wsv(wdata + 4);

  std::vector<sf::Text> texts;
  texts.emplace_back(wstr, font);
  texts.back().setPosition(sf::Vector2f(10, 0));
  texts.emplace_back(sv, font);
  texts.back().setPosition(sf::Vector2f(10, 40));
  texts.emplace_back(str, font);
  texts.back().setPosition(sf::Vector2f(10, 80));
  texts.emplace_back(wsv, font);
  texts.back().setPosition(sf::Vector2f(10, 120));
  
  for(bool run = true; run;) {
    window.clear();
    for(auto& text : texts) {
      window.draw(text);
    }
    window.display();

    sf::Event event;
    while(window.pollEvent(event)) {
      if(event.type == sf::Event::Closed) {
        run = false; break;
      }
    }
  }
}

Cleaned up formatting and added comments
@codecov
Copy link

codecov bot commented Mar 15, 2023

Codecov Report

Merging #2451 (83779bd) into master (641cf54) will decrease coverage by 0.02%.
The diff coverage is 0.00%.

❗ Current head 83779bd differs from pull request most recent head 9c2c29a. Consider uploading reports for the commit 9c2c29a to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2451      +/-   ##
==========================================
- Coverage   23.15%   23.14%   -0.02%     
==========================================
  Files         212      212              
  Lines       18063    18073      +10     
  Branches     4405     4406       +1     
==========================================
  Hits         4183     4183              
- Misses      13152    13162      +10     
  Partials      728      728              
Impacted Files Coverage Δ
include/SFML/System/String.hpp 100.00% <ø> (ø)
src/SFML/System/String.cpp 13.33% <0.00%> (-0.67%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 641cf54...9c2c29a. Read the comment docs.

@khatharr
Copy link
Author

Okay, I don't know what the formatter wants, then. it appears to be objecting to my empty lines and replacing them with its own empty lines. I checked the line endings and they're uniform with the rest of the file. shrug

@ChrisThrasher
Copy link
Member

ChrisThrasher commented Mar 15, 2023

Okay, I don't know what the formatter wants, then. it appears to be objecting to my empty lines and replacing them with its own empty lines. I checked the line endings and they're uniform with the rest of the file. shrug

It's saying that you have trailing whitespace on those lines that needs to be removed. A line with on code on it should not contain 4 spaces. It should contain precisely one newline character and that is all.

You an build the format target to run the autoformatter if you don't want to manually copy its advice into your editor. It's all automated via clang-format.

@khatharr
Copy link
Author

I've never used clang. Is this something I can/should do on github?

@ChrisThrasher
Copy link
Member

ChrisThrasher commented Mar 15, 2023

I've never used clang. Is this something I can/should do on github?

You're better off just copying the output from the failed CI job then. If you're not on Windows, clang-format (which is different from clang) is very easy to install and use but on Windows it may be harder.

@khatharr
Copy link
Author

khatharr commented Mar 15, 2023

Okay. I think I've got it sorted out now.
Should I push another commit, or just nuke this and submit another PR?

Actually, if clang-format is standalone then I'll look at running it from my RPi before uploading anything new.

@eXpl0it3r eXpl0it3r added this to Backlog in SFML 3.0.0 via automation Mar 15, 2023
@eXpl0it3r eXpl0it3r added this to the 3.0 milestone Mar 15, 2023
@eXpl0it3r eXpl0it3r moved this from Backlog to C++17 Changes in SFML 3.0.0 Mar 15, 2023
@ChrisThrasher
Copy link
Member

Okay. I think I've got it sorted out now. Should I push another commit, or just nuke this and submit another PR?

Please do not make another PR. That's not the right way to use GitHub. Just add another commit to this PR.

@khatharr
Copy link
Author

khatharr commented Mar 15, 2023 via email

@ChrisThrasher
Copy link
Member

You can still have single-commit PRs if you like. You can amend a commit in a branch and do a force push to update a PR without adding a 2nd commit. There are various options that are better than making a new PR every time you need to make a single change.

@ChrisThrasher ChrisThrasher changed the title (v2) Adding support for std::string_view to sf::String Adding support for std::string_view to sf::String Mar 15, 2023
Copy link
Member

@ChrisThrasher ChrisThrasher left a comment

Choose a reason for hiding this comment

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

I'd like to hold off on this PR until we can merge #2466. Currently sf::String has no tests which makes me hesitant to add features. Otherwise I think I like this.

@vittorioromeo
Copy link
Member

vittorioromeo commented Apr 4, 2023

I'd like to hold off on this PR until we can merge #2466. Currently sf::String has no tests which makes me hesitant to add features. Otherwise I think I like this.

@ChrisThrasher: should also wait for #2480 and #2470?

@ChrisThrasher
Copy link
Member

Yeah I think I'd prefer it wait if anything just to avoid merge conflicts. I apologize for the delays. This PR was part of what inspired me to look into sf::String in the first place. I expect #2480 to get merged soon.

@eXpl0it3r
Copy link
Member

This can be rebased onto master now 🙂

@khatharr
Copy link
Author

Is that something I need to do, or can someone who knows what needs to be done take care of it?

@ChrisThrasher
Copy link
Member

Is that something I need to do, or can someone who knows what needs to be done take care of it?

It's something that you ought to do

@khatharr
Copy link
Author

How do I do it?

@ChrisThrasher
Copy link
Member

How do I do it?

https://learngitbranching.js.org

This website has tutorials on Git rebasing you can learn from.

@khatharr
Copy link
Author

Thanks!

modified String to integrate with the u32char_t changes and copied over existing project with current state of master
@khatharr
Copy link
Author

Ack, I didn't mean for that to come here.

@khatharr
Copy link
Author

khatharr commented Apr 13, 2023

Okay, so I reverted that and then told it to update from SFML/master, then I resolved the conflicts and looked over it and I think that what I have locally is correct, but when I try to build it I get:

FAILED: src/SFML/Graphics/CMakeFiles/sfml-graphics.dir/ImageLoader.cpp.obj C:\PROGRA~2\MIB055~1\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\cl.exe /nologo /TP -DSFML_GRAPHICS_EXPORTS -DSTBI_FAILURE_USERMSG -D_CRT_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS -I..\..\..\include -I..\..\..\src -I..\..\..\extlibs\headers\stb_image -I..\..\..\extlibs\headers\glad\include -I..\..\..\extlibs\headers\freetype2 /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /WX /W4 /w14242 /w14254 /w14263 /w14265 /w14287 /we4289 /w14296 /w14311 /w14545 /w14546 /w14547 /w14549 /w14555 /w14619 /w14640 /w14826 /w14905 /w14906 /w14928 /permissive- /wd4068 /wd4505 /wd4800 -std:c++17 /showIncludes /Fosrc\SFML\Graphics\CMakeFiles\sfml-graphics.dir\ImageLoader.cpp.obj /Fdsrc\SFML\Graphics\CMakeFiles\sfml-graphics.dir\ /FS -c ..\..\..\src\SFML\Graphics\ImageLoader.cpp C:\Users\Khatharr\Desktop\SFML\extlibs\headers\stb_image\stb_image.h(2224): error C2220: the following warning is treated as an error C:\Users\Khatharr\Desktop\SFML\extlibs\headers\stb_image\stb_image.h(2224): warning C4242: 'argument': conversion from 'int' to 'short', possible loss of data C:\Users\Khatharr\Desktop\SFML\extlibs\headers\stb_image\stb_image.h(2281): warning C4242: 'argument': conversion from 'int' to 'short', possible loss of data C:\Users\Khatharr\Desktop\SFML\extlibs\headers\stb_image\stb_image.h(3425): warning C4242: '=': conversion from 'int' to 'unsigned char', possible loss of data

Should this be building cleanly?

@ChrisThrasher
Copy link
Member

Should this be building cleanly?

Yes. Update to CMake 3.24 or newer to fix it. It's an issue with MSVC not supporting system headers until recently. You can disable WARNINGS_AS_ERRORS as a temporary fix until you can upgrade CMake. I believe the latest version of Visual Studio ships with a sufficiently new CMake so try upgrading VS.

@ChrisThrasher
Copy link
Member

Screenshot 2023-04-12 at 8 26 01 PM

The PR now includes a ton of changes that should be here which means the rebase you did must have not quite worked.

@khatharr
Copy link
Author

khatharr commented Apr 13, 2023

I can't find a way to get rid of the screw-up I made earlier. I have a branch called "string_view" on my repo that has what I want in it, and I'd like to get it to just take the place of my "master" on this PR, but there doesn't seem to be a way to wipe out the history of my screw ups in "master". I can revert the change I made, but it still sees that as 76 changed files, etc.

I can make a new PR to solve this quickly (I'm sorry, I know that's annoying) but I can't figure out any other way to fix this at present.

I'm going to try to push a revert and see what it does.

commit 5c1c795
Author: Khatharr <khatharr@users.noreply.github.com>
Date:   Wed Apr 12 19:49:59 2023 -0700

    Support for string_view

    Adds support to sf::String to allow construction from std::string_view and variants thereof

commit 82b9821
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Thu Feb 2 23:31:48 2023 -0700

    Add clang-tidy `bugprone-*` checks

commit 9269edc
Author: binary1248 <binary1248@hotmail.com>
Date:   Fri Mar 10 02:05:42 2023 +0100

    Add OpenGL ES jobs to ci.yml.

commit 705307f
Author: binary1248 <binary1248@hotmail.com>
Date:   Wed Apr 12 01:58:51 2023 +0200

    Skip OpenGL version printout when test-sfml-window does not exist

commit 913d735
Author: binary1248 <binary1248@hotmail.com>
Date:   Wed Apr 12 01:37:05 2023 +0200

    Don't repeatedly run test setup code

commit 038030a
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Fri Apr 7 21:51:29 2023 -0600

    Fix how CTest config is specified

commit 7a8192a
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Fri Apr 7 20:42:54 2023 -0600

    Fix bug where CTest failing did not fail CI job

commit 1da3e80
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Fri Apr 7 22:42:12 2023 -0600

    Disable Xcode code signing via configuration parameter

commit 2d6d4a4
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Mon Jan 30 15:10:18 2023 -0700

    Allow for manually triggering CI

commit 6692284
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Sat Apr 8 15:22:38 2023 -0600

    Force `brew install` to succeed

commit cafaa1c
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Sat Apr 8 12:51:35 2023 -0600

    Add clang-tidy `performance-*` checks

commit ea4c448
Author: binary1248 <binary1248@hotmail.com>
Date:   Sun Mar 26 14:21:21 2023 +0200

    Add support for installing and using the Mesa 3D library for OpenGL rendering.

commit 0700119
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Thu Apr 6 08:45:46 2023 -0600

    Clean up namespace usage

    Always use std:: and always omit sf::

commit f5e6c67
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Tue Jan 31 14:49:40 2023 -0700

    Remove unnecessary `std::string::c_str()` calls

commit 61aeff1
Author: binary1248 <binary1248@hotmail.com>
Date:   Thu Apr 6 04:00:01 2023 +0200

    Fixed wglGetProcAddress not providing OpenGL 1.1 functions when the context is provided by an Nvidia ICD.

commit 485d367
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Sun Apr 2 15:14:31 2023 -0600

    Inline `sfml_add_external`

commit 21af6fe
Author: binary1248 <binary1248@hotmail.com>
Date:   Sun Mar 26 04:44:05 2023 +0200

    Fixed OpenGL entry points being loaded from OpenGL32.dll instead of failing to load if they are not provided by the vendor provided library on Windows.

commit 2c99b33
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Mon Sep 5 23:18:29 2022 -0600

    Remove default `sf::Text` constructor

commit 57a40c5
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Mon Sep 5 23:08:54 2022 -0600

    Be explicit about when new keys are added to the map

commit f3aac01
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Wed Mar 1 22:50:45 2023 -0700

    Use structured bindings

commit f371a99
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Sat Mar 25 22:02:29 2023 -0600

    Implement `sf::String` in terms of `std::u32string`

commit 93a8506
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Thu Mar 30 08:21:50 2023 -0600

    Let compiler generated special member functions

commit 988a38e
Author: Vittorio Romeo <vittorio.romeo@outlook.com>
Date:   Tue Apr 4 15:54:41 2023 +0200

    Improve PCH by adding more commonly used headers

commit 6a3feda
Author: Jonathan De Wachter <dewachter.jonathan@gmail.com>
Date:   Wed Sep 28 18:43:10 2016 +0700

    Make EGL pixel format selection follow the same procedure as the other context types and make OpenGL context version parsing more tolerant of garbage data. Fixes SFML#2395

commit 7004db1
Author: binary1248 <binary1248@hotmail.com>
Date:   Fri Mar 10 01:38:14 2023 +0100

    Added sf::RenderWindow, sf::Shader and sf::Window tests.

commit ebe4b3c
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Wed Mar 22 21:40:17 2023 -0600

    Add tests for `sf::String`

commit 14dbd3c
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Tue Mar 28 21:48:20 2023 -0600

    Remove old workaround for MinGW's lack of unicode support

    Dates back to 2009 when 78247bd was written. I'm going to assume
    the situation has improved in the last 14 years. There are SFML
    users who weren't even alive in 2009...

commit 06c5c50
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Tue Mar 28 21:27:51 2023 -0600

    De-`constexpr` `sf::String::InvalidPos`

    This fails to link on MinGW. Until that can be resolved we can go
    back to a normal constant. Because this is just an integer it doesn't
    matter all that much whether it's const or constexpr.

commit 741fe21
Author: Jonny <jonathan.r.paton@googlemail.com>
Date:   Mon Apr 3 22:36:33 2023 +0100

    Use built-in CMake support for iOS

    * Use built-in iOS support for cmake and expand tests to cover more configurations
    * Adjust CI builds
    * Update examples version

    ---------

    Co-authored-by: Chris Thrasher <chrisjthrasher@gmail.com>

commit 92e5a1e
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Thu Mar 30 23:30:30 2023 -0600

    Implement move semantics for `sf::Cursor`

commit d500eee
Merge: 5048c4d 5ebba36
Author: Lukas Dürrenberger <eXpl0it3r@sfml-dev.org>
Date:   Sun Apr 2 00:30:16 2023 +0200

    Merge branch '2.6.x' into master

commit 5ebba36
Merge: 5048c4d 541e83e
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Sat Apr 1 15:23:48 2023 -0600

    Merge branch '2.6.x' into feature/backmerge

commit 541e83e
Author: binary1248 <binary1248@hotmail.com>
Date:   Sat Apr 1 02:48:15 2023 +0200

    Disable stringop-overflow warning when including minimp3 since it produces false positives.

commit 5048c4d
Author: binary1248 <binary1248@hotmail.com>
Date:   Fri Mar 10 02:05:53 2023 +0100

    Added sf::Texture tests.

commit a8bc8cf
Author: Jim-Marsden <47166310+Jim-Marsden@users.noreply.github.com>
Date:   Sat Nov 19 21:06:35 2022 -0800

    Added move constructor, and move assignment operator.

commit 2d2f684
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Thu Mar 23 14:13:52 2023 -0600

    Fix clang-tidy-16 errors

commit 2151ea5
Author: Lukas Dürrenberger <eXpl0it3r@my-gate.net>
Date:   Tue Mar 28 22:04:50 2023 +0200

    Align search box styling with the doc style

    - Update doxyfile.in file to v1.9.6
    - Add additional search box CSS overrides

commit 25da983
Author: binary1248 <binary1248@hotmail.com>
Date:   Sun Mar 26 04:41:26 2023 +0200

    Fixed GlContext not being activated if it shares the same address as a previously destroyed GlContext.

commit acba3e6
Author: binary1248 <binary1248@hotmail.com>
Date:   Sun Mar 26 14:09:33 2023 +0200

    Replaced xvfb-run with manually starting Xvfb and running fluxbox to emulate a window manager and combined test and coverage steps into a single step.

commit ccda8a9
Author: binary1248 <binary1248@hotmail.com>
Date:   Fri Mar 17 03:04:11 2023 +0100

    Reduce TransientContextLock overhead if there is already a context active on the current thread.

commit 05d9f20
Author: binary1248 <binary1248@hotmail.com>
Date:   Fri Mar 24 02:18:46 2023 +0100

    Since the CODECOV_TOKEN secret isn't being made available to every pull request, revert to having the Codecov uploader auto-detect.

commit 1bb494f
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Wed Mar 22 22:02:30 2023 -0600

    Upgrade to stb_image 2.28

commit b510042
Author: binary1248 <binary1248@hotmail.com>
Date:   Thu Mar 23 02:25:36 2023 +0100

    Fixed Codecov uploader being run with the SHA of the merge commit instead of the HEAD commit when a job is run in a pull request context.

commit bc5ddb3
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Wed Mar 22 22:08:43 2023 -0600

    Don't automatically build docs

    This means you can leave the docs enabled in the build without being
    constantly spammed by all the console output that entails.

    https://salsa.debian.org/games-team/libsfml/-/blob/master/debian/patches/02_build-doc-once.patch

commit 2df7984
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Wed Mar 22 22:09:33 2023 -0600

    Remove commented out CSS tag

    https://salsa.debian.org/games-team/libsfml/-/blob/master/debian/patches/01_remove-googleapi-css.patch

commit 1058448
Author: Chris Thrasher <chrisjthrasher@gmail.com>
Date:   Tue Mar 21 15:05:46 2023 -0600

    Export symbol required to use sfml-activity

commit 6ca627e
Author: binary1248 <binary1248@hotmail.com>
Date:   Thu Mar 16 04:02:39 2023 +0100

    Manually upload coverage report instead of using codecov-action.
@khatharr
Copy link
Author

khatharr commented Apr 13, 2023

Okay, I think I have it... Let's see how it likes this...

:( No good.

@ChrisThrasher
Copy link
Member

You can open a new PR if that's what you need

@khatharr
Copy link
Author

Moved to #2517

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
SFML 3.0.0
  
C++17 Changes
Development

Successfully merging this pull request may close these issues.

None yet

4 participants