Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
filetools
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
namark
filetools
Commits
c638b9a3
Commit
c638b9a3
authored
7 years ago
by
namark
Browse files
Options
Downloads
Patches
Plain Diff
Improvement is crop:
making use of range type and string to number/range parsers from simple/support.
parent
f7aadb33
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
crop.cpp
+8
-38
8 additions, 38 deletions
crop.cpp
with
8 additions
and
38 deletions
crop.cpp
+
8
−
38
View file @
c638b9a3
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
#include
<string>
#include
<string>
#include
"simple/file.hpp"
#include
"simple/file.hpp"
#include
"simple/support/enum.hpp"
#include
"simple/support/enum.hpp"
#include
"simple/support/misc.hpp"
using
namespace
simple
;
using
namespace
simple
;
using
namespace
std
::
literals
;
using
namespace
std
::
literals
;
...
@@ -66,34 +67,7 @@ template <> Color::Guts::map_type Color::Guts::map
...
@@ -66,34 +67,7 @@ template <> Color::Guts::map_type Color::Guts::map
{
"white"
s
}
{
"white"
s
}
}};
}};
std
::
pair
<
size_t
,
size_t
>
stoull_pair
(
const
std
::
string
&
str
)
using
const_range
=
support
::
range
<
file
::
buffer
<>::
const_iterator
>
;
{
size_t
separator
;
auto
first
=
std
::
stoull
(
str
,
&
separator
,
0
);
auto
second
=
std
::
stoull
(
str
.
substr
(
separator
+
1
),
nullptr
,
0
);
if
(
':'
==
str
[
separator
])
second
+=
first
;
return
{
first
,
second
};
}
template
<
typename
Iterator
>
struct
iterator_range
{
using
iterator_type
=
Iterator
;
using
array_type
=
std
::
array
<
iterator_type
,
2
>
;
iterator_type
&
begin
()
{
return
raw
[
0
];
}
iterator_type
&
end
()
{
return
raw
[
1
];
}
const
iterator_type
&
begin
()
const
{
return
raw
[
0
];
}
const
iterator_type
&
end
()
const
{
return
raw
[
1
];
}
array_type
raw
;
};
template
<
typename
Iterator
>
iterator_range
<
Iterator
>
make_iterator_range
(
Iterator
begin
,
Iterator
end
)
{
return
{
begin
,
end
};
}
using
const_range
=
iterator_range
<
file
::
buffer
<>::
const_iterator
>
;
void
print
(
const_range
range
,
Format
format
,
Color
color
=
Colors
::
Invalid
)
void
print
(
const_range
range
,
Format
format
,
Color
color
=
Colors
::
Invalid
)
{
{
...
@@ -132,15 +106,11 @@ void print(const_range range, Format format, Color color = Colors::Invalid)
...
@@ -132,15 +106,11 @@ void print(const_range range, Format format, Color color = Colors::Invalid)
void
crop
(
const
string
&
filename
,
const
string
&
range
,
Color
color
,
size_t
padding
,
Format
format
)
void
crop
(
const
string
&
filename
,
const
string
&
range
,
Color
color
,
size_t
padding
,
Format
format
)
{
{
auto
p
=
stoull_pair
(
range
);
auto
r
=
support
::
storn
<
size_t
>
(
range
);
auto
source
=
file
::
dump
(
file
::
bropex
(
filename
));
const
auto
source
=
file
::
dump
(
file
::
bropex
(
filename
));
auto
lower
=
std
::
min
(
source
.
begin
()
+
p
.
first
,
source
.
end
());
print
(
support
::
get_iterator_range
(
source
,
{
r
.
lower
()
-
padding
,
r
.
lower
()}),
format
);
auto
upper
=
std
::
min
(
source
.
begin
()
+
p
.
second
,
source
.
end
());
print
(
support
::
get_iterator_range
(
source
,
r
),
format
,
color
);
auto
left
=
lower
-
std
::
min
(
p
.
first
,
padding
);
print
(
support
::
get_iterator_range
(
source
,
{
r
.
upper
(),
r
.
upper
()
+
padding
}),
format
);
auto
right
=
upper
+
std
::
min
<
size_t
>
(
source
.
end
()
-
upper
,
padding
);
print
({
left
,
lower
},
format
);
print
({
lower
,
upper
},
format
,
color
);
print
({
upper
,
right
},
format
);
}
}
void
process_arguments
(
std
::
deque
<
string
>
args
)
void
process_arguments
(
std
::
deque
<
string
>
args
)
...
@@ -157,7 +127,7 @@ void process_arguments(std::deque<string> args)
...
@@ -157,7 +127,7 @@ void process_arguments(std::deque<string> args)
case
Options
::
Padding
:
case
Options
::
Padding
:
args
.
pop_front
();
args
.
pop_front
();
padding
=
s
toull
(
args
.
at
(
0
),
nullptr
,
0
);
padding
=
s
upport
::
ston
<
size_t
>
(
args
.
at
(
0
)
);
break
;
break
;
case
Options
::
Color
:
case
Options
::
Color
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment