'std::out_of_range' on substr string - c++

I got a small problem in one of my scripts.
I have a connection to a websocket and the messages are sent by server to my client.
The format is json. I wrote a simple interpreter which is searching for the quotes.
The function header:
ProcessEvent( string msg )
The function starts like this:
size_t hasType = msg.find("type");
size_t length = msg.size();
/* type */
string date, name, message, color, room, roomname;
int pos = msg.find("type");
if(pos!=std::string::npos) {
msg = msg.substr(pos+7);
int pos2 = msg.find_first_of("\"");
string type = msg.substr(0, pos2);
msg = msg.substr(pos2);
The basic problem is on that part:
/* message */
pos = msg.find("message");
if(pos!=std::string::npos) {
msg = msg.substr(pos+10);
pos2 = msg.find_first_of("\"");
cout << msg << endl;
CONSOLE_Print("size: "+UTIL_ToString(msg.size())+", pos2: "+UTIL_ToString(pos2));
message = msg.substr(0, pos2);
msg = msg.substr(pos2);
} else { message = "empty"; }
As you can see i tried already to debug into this problem.
The problem only occurs on messages with a huge length (this is the only thing coming up from data).
The return of the second debug is this:
size: 949, pos2: -1
The problem is now, that the example input into the client is this:
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
amet.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at
vero eros et accumsan et iusto odio dignissim qui blandit praesent
luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem
vel eum iriure dolor in hendrerit in vulputate velit esse molestie
consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et
accumsan et iusto odio dignissim qui blandit praesent luptatum zzril
delenit augue duis dolore te feugait nulla facilisi.
Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer
This are 300 chars Lorem Ipsum. The text if the debug print isn't 300 chars anymore:
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
amet. Duis autem vel eum iriure dolor in hendrerit in vulputate
That is also obviously the reason why it couldnt find my trailing quote for this message. On the first text there isnt a trailing quote also, i just used to copy the text from my input, this isnt the normal format. The main problem is not the missing quote, its that the string is stripped without a reason.
The whole exception is this:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Aborted
What is the reason that the string is cut like this?

Related

Match string from ">" up to the last dot

I have to select only the characters contained from > to the last dot (not the first dot).
I tried this pattern
^>[a-zA-Z]+$
but something doesn't work. Can I get some help? Thank you.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.
>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.
I made an example using javascript to have a working demo but since I had to use a strategy where the commonly used . (dot) should match also line breaks, in this case I was forced to use [\s\S] instead.
the regex ^>[\s\S]+\.\n expects to find a > at the beginning of the line followed by any character until the last dot found followed by a new line.
This demo feeds the regex match with the full text and returns just the middle part as you were expecting:
const subject = `
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.
>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat.`;
var re = /^>[\s\S]+\.\n/im;
var match = re.exec(subject);
if (match != null) {
result = match[0];
} else {
result = "";
}
console.log(result);
C# .Net Solution
(>[\s\S]*\.)
Or if you don't want to capture the > and . then you can use a positive look behind and positive look ahead.
To match all characters and whitespace in between we can use [\s\S]* This works by default in .Net due to aggressive matching
(?<=>)([\s\S]*)(?=\.)
Try this fiddle: https://dotnetfiddle.net/3ukM0X
public static void Main()
{
string content = #"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
"">""Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.";
var regex = new System.Text.RegularExpressions.Regex(#"(?<=>)([\s\S]*)(?=\.)");
Console.WriteLine(regex.Match(content));
}
Returns:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat
If there can not be any > chars in the paragraph, only at the start, then you can use:
^>[^>]*\.
^ Start of string
> Match literally
[^>]* Match optional chars other than >
\. backtrack to match the last occurrence of the dot
See a regex demo
If you want to allow the > char in the paragraph (but not at the start as that denotes the start of the paragraph) you can match all lines after it that do not start with <
^>.*(?:\r?\n(?!>).*)*\.
See another regex demo

SwiftUI - inline style text background color that fits to the width of multi line text SwiftUI

I am trying to create a background text color that tracks to the width of each line of multi line text. See image below:
I have only been able to get a basic box background color using .background(Color.black)
it gives a background to the entire text box. The desired effect is to give the background only to the end of each line of text.
I only know how to solve this in PHP/CSS.
First, put the text into a variable.
then replace all spaces through the div tags. then add the CSS for the div. and that's it:
!Important! be sure that at the beginning and the is no space and in the middle of the text are not enter space
Here my code example:
<style>
.divs{
background-color: black;
color: white;
margin: 5px 0px 5px 0px;/* space from top and bottom*/
padding-left: 5px;/* word spacing */
float: left;/* Importand to stick them together */
font-size: 20pt;
}
</style>
<div>
<?php
$yourtext = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
echo "<div class='divs'>".str_replace(" ", "</div> <div class='divs'>", $yourtext)."</div>";
?>
</div>
But I'm sure that the same method is possible with SwiftUI by just looping the text and replace the space
--> just search for text-replacing in SwiftUI and you will find what you need

Regular expression to select group of text/bunch of lines based on specific line of text

I have the text in below format
Scenario: Lorem ipsum dolor sit amet, consectetur adipiscing elit
Given sed do eiusmod tempor
When Ut enim ad minim veniam, quis nostrud exercitation
Then In voluptate velit esse cillum dolore eu fugiat nulla
#Sprint4 #543fsdf
Scenario: Lorem ipsum dolor sit amet, consectetur adipiscing elit
Given sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
When Ut enim ad minim veniam, quis nostrud exercitation
Then In voluptate velit esse cillum dolore eu fugiat nulla
#Sprint1 #345277
Scenario: Sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt returned correct singular message
Given sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
And Duis aute irure dolor in reprehenderit
When Ut enim ad minim veniam, quis nostrud
Then quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur
#Sprint1 #xyz1234
Scenario: At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis
Given sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
And Duis aute irure dolor in reprehenderit
When Ut enim ad minim veniam, quis nostrud
Then quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur
I want to select whole scenario based on a line of text in it.
For eg.
based on the line 'Given sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'
I want the second scenario
Scenario: Lorem ipsum dolor sit amet, consectetur adipiscing elit
Given sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
When Ut enim ad minim veniam, quis nostrud exercitation
Then In voluptate velit esse cillum dolore eu fugiat nulla
, also the third and fourth scenario - but not the first one
I am trying
Scenario[\s\S]*?Given sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua[\s\S]*?(?=(#|Scenario))
But that gives me first and second scenarios together and third but no fourth scenario. I tried other patterns as well but not a lot of success. Help?
Given that your Scenario always begin on a new line, you can use look-behind to search for that, and you can use look-aheat to search for whitespace followed by # or end of the string to capture the end of a matched block. So, for your example:
(?<=\s)Scenario.*?[\r\n]+Given sed do eiusmod tempor incididunt ut labore et dolore magna aliqua[\r\n]+.*?(?=\s+#|\s*$)
You didn't specify what language are you using so you'll have to do the set up, but from options (if not default) you need to choose that dot matches line breaks, and that ^ and $ don't match line breaks but rather the whole string.
I don't know if will help, but what I did was to match all lines with # until the end of line. So you could replace that by null, if this is the case.
(#\V+)
https://regex101.com/r/iCt1j2/1

Use regular expression to grab specific tags

I have a string like this
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
<img src="foo.php?n=bar.jpg">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
<img src="somesource.jpg">
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
<img src="foo.php?n=bar_2.jpg">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
<img src="foo.php?n=bar_93.jpg">
.
I am now looking for a regex that gives me back all the <img> tags where the source contains bar
So the output should be:
<img src="foo.php?n=bar.jpg">
<img src="foo.php?n=bar_2.jpg">
<img src="foo.php?n=bar_93.jpg">
Could anyone help me out please ?
This regex should do it
<img((bar)|[^>])*?>(?(2)(?=)|(?!))
It uses a conditional expression to see if "bar" was matched in group 2

CSS word-wrap / text-overflow with fixed height [duplicate]

This question already has answers here:
Is it possible to use text-overflow:ellipsis on multiline text?
(12 answers)
Closed 9 years ago.
i have the following example.
http://jsfiddle.net/qFDxp/
HTML:
<div class="test">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</div>
CSS:
.test
{
display: block;
height: 299px;
left: 230px;
overflow: hidden;
text-overflow: ellipsis;
top: 110px;
white-space: pre-line;
width: 199px;
z-index: 1;
background-color: #FFFFFF;
border: 1px solid #777777;
position: absolute;
}
What i wish?
Thank you in Advance!
text-overflow: ellipsis only works on single line content as you need the line white-space:nowrap for it to work.
Check this post for some ideas for workarounds