Skip to content

Commit

Permalink
fix: not all character is two width
Browse files Browse the repository at this point in the history
  • Loading branch information
GiveMe-A-Name committed Feb 7, 2025
1 parent a1b6682 commit f13a01f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ pub enum NonNarrowChar {
/// Represents a zero-width character
ZeroWidth(BytePos),
/// Represents a wide (fullwidth) character
Wide(BytePos),
Wide(BytePos, usize),
/// Represents a tab character, represented visually with a width of 4
/// characters
Tab(BytePos),
Expand All @@ -710,24 +710,23 @@ impl NonNarrowChar {
fn new(pos: BytePos, width: usize) -> Self {
match width {
0 => NonNarrowChar::ZeroWidth(pos),
2 => NonNarrowChar::Wide(pos),
4 => NonNarrowChar::Tab(pos),
_ => panic!("width {} given for non-narrow character", width),
w => NonNarrowChar::Wide(pos, w),
}
}

/// Returns the absolute offset of the character in the SourceMap
pub fn pos(self) -> BytePos {
match self {
NonNarrowChar::ZeroWidth(p) | NonNarrowChar::Wide(p) | NonNarrowChar::Tab(p) => p,
NonNarrowChar::ZeroWidth(p) | NonNarrowChar::Wide(p, _) | NonNarrowChar::Tab(p) => p,
}
}

/// Returns the width of the character, 0 (zero-width) or 2 (wide)
pub fn width(self) -> usize {
match self {
NonNarrowChar::ZeroWidth(_) => 0,
NonNarrowChar::Wide(_) => 2,
NonNarrowChar::Wide(_, width) => width,
NonNarrowChar::Tab(_) => 4,
}
}
Expand All @@ -739,7 +738,7 @@ impl Add<BytePos> for NonNarrowChar {
fn add(self, rhs: BytePos) -> Self {
match self {
NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos + rhs),
NonNarrowChar::Wide(pos) => NonNarrowChar::Wide(pos + rhs),
NonNarrowChar::Wide(pos, width) => NonNarrowChar::Wide(pos + rhs, width),
NonNarrowChar::Tab(pos) => NonNarrowChar::Tab(pos + rhs),
}
}
Expand All @@ -751,7 +750,7 @@ impl Sub<BytePos> for NonNarrowChar {
fn sub(self, rhs: BytePos) -> Self {
match self {
NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos - rhs),
NonNarrowChar::Wide(pos) => NonNarrowChar::Wide(pos - rhs),
NonNarrowChar::Wide(pos, width) => NonNarrowChar::Wide(pos - rhs, width),
NonNarrowChar::Tab(pos) => NonNarrowChar::Tab(pos - rhs),
}
}
Expand Down

0 comments on commit f13a01f

Please sign in to comment.